Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

if opts.projectName is defined, prevent to read the pkgPath file #48

Closed
ctxhou opened this issue Oct 7, 2018 · 3 comments
Closed

if opts.projectName is defined, prevent to read the pkgPath file #48

ctxhou opened this issue Oct 7, 2018 · 3 comments

Comments

@ctxhou
Copy link

ctxhou commented Oct 7, 2018

Hi, I would like to suggest whether conf can prevent read the pkgPath file if the opts.projectName is defined.

Now the lib uses this way to set the pkgPath link

options = Object.assign({
  // Can't use `require` because of Webpack being annoying:
  // https://github.com/webpack/webpack/issues/196
  projectName: pkgPath && JSON.parse(fs.readFileSync(pkgPath, 'utf8')).name
}, options);

if (!options.projectName && !options.cwd) {
  throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}

I think whether it can exam the option first?
The reason is I use the electron-store, and in some cases, I don't know why it loads the pkgPath as root system path (e.g.: /Users/<UserName>/pacakge.json). This will cause the permission issues.
If we can exam the projectName first, we can prevent the conf to automatically load the pkgPath.

So the change would be:

options = Object.assign({
  // Can't use `require` because of Webpack being annoying:
  // https://github.com/webpack/webpack/issues/196
  projectName: options.projectName ? options.projectName : pkgPath && JSON.parse(fs.readFileSync(pkgPath, 'utf8')).name
}, options);

if (!options.projectName && !options.cwd) {
  throw new Error('Project name could not be inferred. Please specify the `projectName` option.');
}
@sindresorhus
Copy link
Owner

Sure, PR welcome, but really no point in having it in the Object.assign call at all then. Can just be:

options = Object.assign({}, options);

if (!options.projectName) {
	// Can't use `require` because of Webpack being annoying:
	// https://github.com/webpack/webpack/issues/196
	options.projectName = pkgPath && JSON.parse(fs.readFileSync(pkgPath, 'utf8')).name;
}

@sindresorhus
Copy link
Owner

The reason is I use the electron-store, and in some cases, I don't know why it loads the pkgPath as root system path (e.g.: /Users//pacakge.json).

Can you reproduce the issue reliably? Would be good to get that fixed for real instead of just hiding it away.

@ctxhou
Copy link
Author

ctxhou commented Oct 16, 2018

Yeah, I also hope to fix the real problem, but it's hard for me to reproduce, because I only received this error report from one of the users. I will write down all things I knew so that we can discuss the possible bug.


Here is the case:

  • The app is published on Mac App Store
  • Mac version: High Sierra (10.13)

Error message:

Uncaught Exception:
Error: EPERM: operation not permitted, open '/Users/<username>/package.json'
	at Object.fs.openSync(fs.js:645:18)
	at Object.module(anonymous function)[as openSync]
	at Object.fs.readFileSync(fs.js:551:33)
...

Here is how I declare the electron-store. It's the same with the document.

import Store from 'electron-store';
const store = new Store();

I think the possible reason is that pkgPath guess the wrong path. However, this is a special case. It only occurred in this user's case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants