{"action":"created","issue":{"url":"https://api.github.com/repos/nwjs/nw.js/issues/5582","repository_url":"https://api.github.com/repos/nwjs/nw.js","labels_url":"https://api.github.com/repos/nwjs/nw.js/issues/5582/labels{/name}","comments_url":"https://api.github.com/repos/nwjs/nw.js/issues/5582/comments","events_url":"https://api.github.com/repos/nwjs/nw.js/issues/5582/events","html_url":"https://github.com/nwjs/nw.js/issues/5582","id":197219798,"number":5582,"title":"Path resolving bug, when .js is used as \"main\" script","user":{"login":"ggarek","id":5501062,"avatar_url":"https://avatars.githubusercontent.com/u/5501062?v=3","gravatar_id":"","url":"https://api.github.com/users/ggarek","html_url":"https://github.com/ggarek","followers_url":"https://api.github.com/users/ggarek/followers","following_url":"https://api.github.com/users/ggarek/following{/other_user}","gists_url":"https://api.github.com/users/ggarek/gists{/gist_id}","starred_url":"https://api.github.com/users/ggarek/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ggarek/subscriptions","organizations_url":"https://api.github.com/users/ggarek/orgs","repos_url":"https://api.github.com/users/ggarek/repos","events_url":"https://api.github.com/users/ggarek/events{/privacy}","received_events_url":"https://api.github.com/users/ggarek/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":4,"created_at":"2016-12-22T17:00:56Z","updated_at":"2016-12-27T02:18:03Z","closed_at":null,"body":"# Start\r\nFirst of all i want to say, that i am aware of \"different contexts thing\", and i read several times the explanation.\r\n\r\n**But**, i still think that there is a corner case:\r\n\r\n# The case\r\n## Content\r\n```\r\n(master)⚡ % tree nw-path-bug\r\nnw-path-bug\r\n├── a\r\n│   └── index.js\r\n├── b\r\n│   └── data.json\r\n└── package.json\r\n\r\n2 directories, 3 files\r\n```\r\n```\r\n(master)⚡ % cat nw-path-bug/package.json\r\n{\r\n  \"name\": \"seems there is a bug\",\r\n  \"version\": \"1.0.0\",\r\n  \"main\": \"a/index.js\"\r\n}\r\n```\r\n```\r\n(master)⚡ % cat nw-path-bug/a/index.js\r\nconst fs = require('fs');\r\n\r\n// Works, but the path is wrong!\r\nfs.readFileSync('./b/data.json');\r\n\r\n// Fails, though path is correct\r\nfs.readFileSync('../b/data.json');%\r\n```\r\n\r\n## Try to run it\r\n```\r\n(master)⚡ % ~/lib/nwjs/nwjs-v0.20.0-beta1-osx-x64/nwjs.app/Contents/MacOS/nwjs nw-path-bug/\r\nfs.js:557\r\n  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);\r\n                 ^\r\n\r\nError: ENOENT: no such file or directory, open '../b/data.json'\r\n    at Object.fs.openSync (fs.js:557:18)\r\n    at Object.fs.readFileSync (fs.js:467:33)\r\n    at chrome-extension://lghcaicggppoaecmnfacgidenooinold/a/index.js:7:4\r\n```\r\n```\r\n(master)⚡ % ~/lib/nwjs/nwjs-sdk-v0.19.2/nwjs.app/Contents/MacOS/nwjs nw-path-bug/\r\nfs.js:557\r\n  return binding.open(pathModule._makeLong(path), stringToFlags(flags), mode);\r\n                 ^\r\n\r\nError: ENOENT: no such file or directory, open '../b/data.json'\r\n    at Object.fs.openSync (fs.js:557:18)\r\n    at Object.fs.readFileSync (fs.js:467:33)\r\n    at chrome-extension://lghcaicggppoaecmnfacgidenooinold/a/index.js:7:4\r\n```\r\n\r\n# Additional info\r\nI checked with 2 nwjs versions:\r\n- nwjs-v0.20.0-beta1-osx-x64\r\n- nwjs-sdk-v0.19.2\r\n\r\nI run OSX 10.11.6 (15G1108)\r\n\r\nin both cases i use\r\n```\r\n(master)⚡ % node -v\r\nv7.2.1\r\n```\r\n\r\n# Example/Reporduction\r\n[nw-path-bug.zip](https://github.com/nwjs/nw.js/files/669319/nw-path-bug.zip)\r\n"},"comment":{"url":"https://api.github.com/repos/nwjs/nw.js/issues/comments/269258856","html_url":"https://github.com/nwjs/nw.js/issues/5582#issuecomment-269258856","issue_url":"https://api.github.com/repos/nwjs/nw.js/issues/5582","id":269258856,"user":{"login":"ghostoy","id":471928,"avatar_url":"https://avatars.githubusercontent.com/u/471928?v=3","gravatar_id":"","url":"https://api.github.com/users/ghostoy","html_url":"https://github.com/ghostoy","followers_url":"https://api.github.com/users/ghostoy/followers","following_url":"https://api.github.com/users/ghostoy/following{/other_user}","gists_url":"https://api.github.com/users/ghostoy/gists{/gist_id}","starred_url":"https://api.github.com/users/ghostoy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ghostoy/subscriptions","organizations_url":"https://api.github.com/users/ghostoy/orgs","repos_url":"https://api.github.com/users/ghostoy/repos","events_url":"https://api.github.com/users/ghostoy/events{/privacy}","received_events_url":"https://api.github.com/users/ghostoy/received_events","type":"User","site_admin":false},"created_at":"2016-12-27T02:18:03Z","updated_at":"2016-12-27T02:18:03Z","body":"@ggarek Sorry, I just reviewed your issue and found out you are talking about `fs.readFileSync` instead of `require`.\r\n\r\nThe relative path resolving is based on current working directory (CWD). And CWD is the root directory of your project (a.k.a. directory of `package.json`), no matter which context your script is loaded in. You can change your CWD to `a` by `process.chdir('a')`. It will make all subsequent relative path resolving based on `a`."}}
{"actor":{"display_login":"ghostoy"}}