{"action":"created","issue":{"url":"https://api.github.com/repos/saltstack/salt/issues/39883","repository_url":"https://api.github.com/repos/saltstack/salt","labels_url":"https://api.github.com/repos/saltstack/salt/issues/39883/labels{/name}","comments_url":"https://api.github.com/repos/saltstack/salt/issues/39883/comments","events_url":"https://api.github.com/repos/saltstack/salt/issues/39883/events","html_url":"https://github.com/saltstack/salt/issues/39883","id":212714895,"number":39883,"title":"file.line function fails with NameError on Windows","user":{"login":"smarsching","id":2880129,"avatar_url":"https://avatars3.githubusercontent.com/u/2880129?v=3","gravatar_id":"","url":"https://api.github.com/users/smarsching","html_url":"https://github.com/smarsching","followers_url":"https://api.github.com/users/smarsching/followers","following_url":"https://api.github.com/users/smarsching/following{/other_user}","gists_url":"https://api.github.com/users/smarsching/gists{/gist_id}","starred_url":"https://api.github.com/users/smarsching/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/smarsching/subscriptions","organizations_url":"https://api.github.com/users/smarsching/orgs","repos_url":"https://api.github.com/users/smarsching/repos","events_url":"https://api.github.com/users/smarsching/events{/privacy}","received_events_url":"https://api.github.com/users/smarsching/received_events","type":"User","site_admin":false},"labels":[{"id":41387723,"url":"https://api.github.com/repos/saltstack/salt/labels/Bug","name":"Bug","color":"e11d21","default":false},{"id":41388135,"url":"https://api.github.com/repos/saltstack/salt/labels/High%20Severity","name":"High Severity","color":"ff9143","default":false},{"id":194766324,"url":"https://api.github.com/repos/saltstack/salt/labels/P4","name":"P4","color":"031a39","default":false},{"id":194768064,"url":"https://api.github.com/repos/saltstack/salt/labels/Platform","name":"Platform","color":"fef2c0","default":false},{"id":58954720,"url":"https://api.github.com/repos/saltstack/salt/labels/Windows","name":"Windows","color":"006b75","default":false}],"state":"closed","locked":false,"assignee":{"login":"twangboy","id":9383935,"avatar_url":"https://avatars0.githubusercontent.com/u/9383935?v=3","gravatar_id":"","url":"https://api.github.com/users/twangboy","html_url":"https://github.com/twangboy","followers_url":"https://api.github.com/users/twangboy/followers","following_url":"https://api.github.com/users/twangboy/following{/other_user}","gists_url":"https://api.github.com/users/twangboy/gists{/gist_id}","starred_url":"https://api.github.com/users/twangboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twangboy/subscriptions","organizations_url":"https://api.github.com/users/twangboy/orgs","repos_url":"https://api.github.com/users/twangboy/repos","events_url":"https://api.github.com/users/twangboy/events{/privacy}","received_events_url":"https://api.github.com/users/twangboy/received_events","type":"User","site_admin":false},"assignees":[{"login":"twangboy","id":9383935,"avatar_url":"https://avatars0.githubusercontent.com/u/9383935?v=3","gravatar_id":"","url":"https://api.github.com/users/twangboy","html_url":"https://github.com/twangboy","followers_url":"https://api.github.com/users/twangboy/followers","following_url":"https://api.github.com/users/twangboy/following{/other_user}","gists_url":"https://api.github.com/users/twangboy/gists{/gist_id}","starred_url":"https://api.github.com/users/twangboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twangboy/subscriptions","organizations_url":"https://api.github.com/users/twangboy/orgs","repos_url":"https://api.github.com/users/twangboy/repos","events_url":"https://api.github.com/users/twangboy/events{/privacy}","received_events_url":"https://api.github.com/users/twangboy/received_events","type":"User","site_admin":false}],"milestone":{"url":"https://api.github.com/repos/saltstack/salt/milestones/98","html_url":"https://github.com/saltstack/salt/milestone/98","labels_url":"https://api.github.com/repos/saltstack/salt/milestones/98/labels","id":2350306,"number":98,"title":"Nitrogen 5","description":"","creator":{"login":"terminalmage","id":328598,"avatar_url":"https://avatars2.githubusercontent.com/u/328598?v=3","gravatar_id":"","url":"https://api.github.com/users/terminalmage","html_url":"https://github.com/terminalmage","followers_url":"https://api.github.com/users/terminalmage/followers","following_url":"https://api.github.com/users/terminalmage/following{/other_user}","gists_url":"https://api.github.com/users/terminalmage/gists{/gist_id}","starred_url":"https://api.github.com/users/terminalmage/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/terminalmage/subscriptions","organizations_url":"https://api.github.com/users/terminalmage/orgs","repos_url":"https://api.github.com/users/terminalmage/repos","events_url":"https://api.github.com/users/terminalmage/events{/privacy}","received_events_url":"https://api.github.com/users/terminalmage/received_events","type":"User","site_admin":false},"open_issues":25,"closed_issues":6,"state":"open","created_at":"2017-02-27T14:49:56Z","updated_at":"2017-03-10T16:12:31Z","due_on":"2017-03-17T07:00:00Z","closed_at":null},"comments":8,"created_at":"2017-03-08T11:58:55Z","updated_at":"2017-03-11T00:33:53Z","closed_at":"2017-03-09T22:19:59Z","body":"### Description of Issue/Question\r\n\r\nThe `file.line` state function is broken on Windows due to a bug in the `win_file` execution module. When using the `file.line` function, execution fails with `NameError: global name '__opts__' is not defined`.\r\n\r\nIn my opinion, this is due to the crazy way how the `win_file` module uses functions from the `file` module, while at the same time replacing the `file` module. The `win_file` module selective imports functions from the `file` module, completely bypassing the Salt loader mechanism. This means that the `__opts__` dunder is not inject into the `file` module and thus not available to its functions.\r\n\r\nThe `win_file` module also imports a lot of internal functions from the `file` module because calling the `file` module’s public functions without these imports would result in `NameError`s as well. This has the crazy effect that internal changes made to the `file` module may actually break the `win_file` module if it is not updated accordingly (e.g. by adding imports for newly added internal functions). I learned this the hard way when writing the patch for #39842.\r\n\r\nFor this reason, I think that effort should be put into cleaning up the `win_file` module and making it work without all these explicit imports of internal functions.\r\n\r\nFrom an architectural point of view, the cleanest way would be making the `file` module purely virtual and having three implementation modules: A `posix_file` module providing `file` on non-Windows platforms, a `win_file` module providing `file` on Windows platforms, and a utility module that implements the functionality shared by the `posix_file` and `win_file` modules.\r\n\r\nHowever, this would be quite a big change and should most likely not be done in a bugfix release. For this reason, it should be investigated whether using the `salt.loader.raw_mod` function for using the `file` module from the `win_file` module might at least fix this issue (and possibly similar undiscovered issues) caused by the current import scheme.\r\n\r\nSuch a change would only have to touch the `win_file` module and thus code that essentially is already broken. A larger refactoring of the `file` code should still be considered for a future major release.\r\n\r\n### Setup\r\n`/srv/salt/test/line_test.sls`:\r\n```\r\nmanaged_file:\r\n  file.line:\r\n    - name: 'C:\\mytest.txt'\r\n    - match: \"Second line\"\r\n    - content: \"Other text\"\r\n    - mode: replace\r\n```\r\n\r\n`C:\\mytest.txt` (on minion):\r\n```\r\nFirst line\r\nSecond line\r\n```\r\n\r\n### Steps to Reproduce Issue\r\nRunning `salt server.example.com state.apply test.line_test` results in:\r\n\r\n```\r\nserver.example.com:\r\n----------\r\n          ID: managed_file\r\n    Function: file.line\r\n        Name: C:\\mytest.txt\r\n      Result: False\r\n     Comment: An exception occurred in this state: Traceback (most recent call last):\r\n                File \"c:\\salt\\bin\\lib\\site-packages\\salt\\state.py\", line 1746, in call\r\n                  **cdata['kwargs'])\r\n                File \"c:\\salt\\bin\\lib\\site-packages\\salt\\loader.py\", line 1703, in wrapper\r\n                  return f(*args, **kwargs)\r\n                File \"c:\\salt\\bin\\lib\\site-packages\\salt\\states\\file.py\", line 3191, in line\r\n                  backup=backup, quiet=quiet, indent=indent)\r\n                File \"c:\\salt\\bin\\lib\\site-packages\\salt\\modules\\file.py\", line 1819, in line\r\n                  if __opts__['test'] is False:\r\n              NameError: global name '__opts__' is not defined\r\n     Started: 12:20:53.455000\r\n    Duration: 32.0 ms\r\n     Changes:   \r\n\r\nSummary for server.example.com\r\n------------\r\nSucceeded: 0\r\nFailed:    1\r\n------------\r\nTotal states run:     1\r\nTotal run time:  32.000 ms\r\n```\r\n\r\n### Versions Report\r\n\r\n```\r\nSalt Version:\r\n           Salt: 2016.11.3\r\n\r\nDependency Versions:\r\n           cffi: 1.7.0\r\n       cherrypy: unknown\r\n       dateutil: 2.5.3\r\n          gitdb: 0.6.4\r\n      gitpython: 2.0.8\r\n          ioflo: 1.5.5\r\n         Jinja2: 2.9.4\r\n        libgit2: Not Installed\r\n        libnacl: 1.4.5\r\n       M2Crypto: Not Installed\r\n           Mako: 1.0.4\r\n   msgpack-pure: Not Installed\r\n msgpack-python: 0.4.8\r\n   mysql-python: Not Installed\r\n      pycparser: 2.14\r\n       pycrypto: 2.6.1\r\n         pygit2: Not Installed\r\n         Python: 2.7.12 (v2.7.12:d33e0cf91556, Jun 27 2016, 15:24:40) [MSC v.150\r\n0 64 bit (AMD64)]\r\n   python-gnupg: 0.3.8\r\n         PyYAML: 3.11\r\n          PyZMQ: 16.0.1\r\n           RAET: Not Installed\r\n          smmap: 0.9.0\r\n        timelib: 0.2.4\r\n        Tornado: 4.4.1\r\n            ZMQ: 4.1.6\r\n\r\nSystem Versions:\r\n           dist:\r\n        machine: AMD64\r\n        release: 2012ServerR2\r\n         system: Windows\r\n        version: 2012ServerR2 6.3.9600  Multiprocessor Free\r\n```\r\n"},"comment":{"url":"https://api.github.com/repos/saltstack/salt/issues/comments/285822400","html_url":"https://github.com/saltstack/salt/issues/39883#issuecomment-285822400","issue_url":"https://api.github.com/repos/saltstack/salt/issues/39883","id":285822400,"user":{"login":"twangboy","id":9383935,"avatar_url":"https://avatars0.githubusercontent.com/u/9383935?v=3","gravatar_id":"","url":"https://api.github.com/users/twangboy","html_url":"https://github.com/twangboy","followers_url":"https://api.github.com/users/twangboy/followers","following_url":"https://api.github.com/users/twangboy/following{/other_user}","gists_url":"https://api.github.com/users/twangboy/gists{/gist_id}","starred_url":"https://api.github.com/users/twangboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/twangboy/subscriptions","organizations_url":"https://api.github.com/users/twangboy/orgs","repos_url":"https://api.github.com/users/twangboy/repos","events_url":"https://api.github.com/users/twangboy/events{/privacy}","received_events_url":"https://api.github.com/users/twangboy/received_events","type":"User","site_admin":false},"created_at":"2017-03-11T00:33:53Z","updated_at":"2017-03-11T00:33:53Z","body":"@smarsching I saw that. Thanks for the detailed write up."}}
{"actor":{"display_login":"twangboy"}}