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

feat(config): throw on invalid config instead of using fallback #111

Closed
Aghassi opened this issue Feb 4, 2023 · 9 comments
Closed

feat(config): throw on invalid config instead of using fallback #111

Aghassi opened this issue Feb 4, 2023 · 9 comments

Comments

@Aghassi
Copy link

Aghassi commented Feb 4, 2023

Description

I believe, and I could be wrong, that when the config read (specifically in the case of a .js config) is malformed, syncpack falls back on and uses a default config. The reason that I think this is the wrong solution is because it can lead to scenarios where in syncpack passes when it should instead fail.

Suggested Solution

If syncpack.config.js fails for any reason (like missing dependencies in my case due to uninstalled node_modules), syncpack needs to bubble up that error and exit with code 1 to inform me that the config is malformed. This would have prevent a large headache for me of trying to figure out why syncpack was running but not failing.

Help Needed

Just confirmation that the behavior I described above is intended and if it is ok with being improved

@JamieMason
Copy link
Owner

JamieMason commented Feb 4, 2023

Hey @Aghassi, that's right.

  1. The config file is read on this line:

    const rcFile = disk.readConfigFileSync(program.configPath);

  2. .js config files are read using cosmicconfig on this line:

    const result = configPath ? client.load(configPath) : client.search();

  3. If the .js config file throws or isn't found then an empty object is returned by disk.readConfigFileSync.

  4. The rest of that getConfig function then attempts to read each config value and uses it only if it's valid, otherwise using a default if missing or invalid.

Possible Approach

There is currently some undocumented logging which can be enabled like so

SYNCPACK_VERBOSE=true syncpack # list/format/fix-mismatches/etc

I think what I'd want to do is:

  1. Add/update logs to make better use of log levels (in descending order of amount: debug, info, warn, error, silent).
  2. Set the default log level to error, so that errors start appearing in output for everyone.
  3. Expose a logLevel in config so that it can be set to silent if needed, to return to how it works today.
  4. Remove SYNCPACK_VERBOSE and make use of the above.

Notes/Questions:

  1. Not being able to find a config file is not an error.
  2. cosmiconfig finding a config file but it being malformed is an error.
  3. Does cosmiconfig let us tell the difference? (I've not checked the docs yet).
  4. The typical CLI output should probably not be affected by the logLevel – running syncpack list with --log-level silent and getting no output would be weird.

I like the idea but it's probably a low priority, compared to some of the other features coming up.

Thanks a lot for this and your other issues, they've all helped improve syncpack a lot.

@Aghassi
Copy link
Author

Aghassi commented Feb 5, 2023

My pleasure! This tool has been invaluable in trying to keep things aligned while we scale and dedupe dependencies. Until or unless we have one workspace these things matter.

Per your questions:

  1. Not being able to find a config file is not an error.

Agreed, having a default makes sense in this case.

  1. cosmiconfig finding a config file but it being malformed is an error.

Agreed here too, because otherwise falling back on the default is confusing

  1. Does cosmiconfig let us tell the difference? (I've not checked the docs yet).

Yes loading the config will reject a Promise, see here https://www.npmjs.com/package/cosmiconfig#explorerload

  1. The typical CLI output should probably not be affected by the logLevel – running syncpack list with --log-level silent and getting no output would be weird.

To rephrase and I think agree, the result of the CLI should ignore the log level, but internal information around debug statements should show based on log level. If so then yes. Without adding another library you can rely on debug from util https://nodejs.org/api/util.html#utildebugsection. Then just set some env variable based on flag.

@JamieMason JamieMason changed the title Change behavior when config is malformed feat(config): throw on invalid config instead of using fallback May 28, 2023
JamieMason added a commit that referenced this issue May 28, 2023
Closes #124

When using the `workspace` dependency type, packages installing that dependency
no longer have to exactly match the `version` property of the package.json of
origin.

If the version or version range used by every dependent package matches, it is
considered valid.

Closes #130

JavaScript config files now have support for TypeScript IntelliSense.

https://jamiemason.github.io/syncpack/config-file#typescript-intellisense

Closes #114
Refs #109
Refs #125

Unsupported versions can now at least be managed via `pinVersion`, where
previously anything which was not valid semver would be ignored.

Refs #111
Refs #132

TypeScript IntelliSense support helps catch invalid config, but more work is
needed to display useful error messages at runtime.

Refs #48
Refs #3

Syncpack's internals are now better organised, so providing a Node.js API and
general lint and fix CLI commands are now closer to being released.

BREAKING CHANGES:

Although they are still not auto-fixable, unsupported versions which were
previously ignored are now acknowledged, which may introduce mismatches which
previously would have been considered valid.

This release was also a huge rewrite of Syncpack's internals and, while there
is a large amount of tests, some scenarios may have been missed.

If you run into any problems, please create an issue.
JamieMason added a commit that referenced this issue May 28, 2023
Closes #124

When using the `workspace` dependency type, packages installing that dependency no longer have to exactly match the `version` property of the package.json of origin.

If the version or version range used by every dependent package matches, it is considered valid.

Closes #130
Closes #131

JavaScript config files now have support for TypeScript IntelliSense.

https://jamiemason.github.io/syncpack/config-file#typescript-intellisense

Closes #114
Refs #109
Refs #125

Unsupported versions can now at least be managed via `pinVersion`, where previously anything which was not valid semver would be ignored.

Refs #111
Refs #132

TypeScript IntelliSense support helps catch invalid config, but more work is needed to display useful error messages at runtime.

Refs #48
Refs #3

Syncpack's internals are now better organised, so providing a Node.js API and general lint and fix CLI commands are now closer to being released.

BREAKING CHANGES:

- `fix-mismatches` will now exit with a status code of 1 if there are mismatches among unsupported versions which syncpack cannot auto-fix.
- Although they are still not auto-fixable, unsupported versions which were previously ignored are now acknowledged, which may introduce mismatches which previously would have been considered valid.
- This release was also a huge rewrite of Syncpack's internals and, while there is a large amount of tests, some scenarios may have been missed.
- If you run into any problems, please create an issue.
JamieMason added a commit that referenced this issue May 28, 2023
### #124

When using the `workspace` dependency type, packages installing that dependency no longer have to exactly match the `version` property of the package.json of origin.

Closes #124

If the version or version range used by every dependent package matches, it is considered valid.

### #130, #131

JavaScript config files now have support for TypeScript IntelliSense.

Closes #130
Closes #131

https://jamiemason.github.io/syncpack/config-file#typescript-intellisense

### #109, #114, #125

Unsupported versions can now at least be managed via `pinVersion`, where previously anything which was not valid semver would be ignored.

Closes #114

### #111, #132

TypeScript IntelliSense support helps catch invalid config, but more work is needed to display useful error messages at runtime.

### #48, #3

Syncpack's internals are now better organised, so providing a Node.js API and general lint and fix CLI commands are now closer to being released.

BREAKING CHANGE:

- `fix-mismatches` will now exit with a status code of 1 if there are mismatches among unsupported versions which syncpack cannot auto-fix.
- Although they are still not auto-fixable, unsupported versions which were previously ignored are now acknowledged, which may introduce mismatches which previously would have been considered valid.
- This release was also a huge rewrite of Syncpack's internals and, while there is a large amount of tests, some scenarios may have been missed.
- If you run into any problems, please create an issue.
JamieMason added a commit that referenced this issue Jun 18, 2023
Closes #140

When linting semver ranges, warn on non-semver versions but don't error

Closes #139

Handle !negated globs

Closes #132

Show more detailed information about errors and warnings

Closes #111

Throw when config is invalid, instead of defaulting
@JamieMason
Copy link
Owner

Released in 10.6.1. Any problems, please let me know.

If you find syncpack useful, please star the project or help us spread the word to other Developers.

@Aghassi
Copy link
Author

Aghassi commented Jun 18, 2023

Solid thanks! I still have to get us to 10.x. I have an issue with rules_js exiting with the incorrect error code and I haven't figured out why yet. Been busy. Will get there though! I'm looking forward to all the improvements :)

@JamieMason
Copy link
Owner

This version might help with that, I've reverted lint semver groups from erroring when it sees a non semver version, which I think I did in V10.

@Aghassi
Copy link
Author

Aghassi commented Jun 19, 2023

This version might help with that, I've reverted lint semver groups from erroring when it sees a non semver version, which I think I did in V10.

Oh man that actually might be it! Ok I'll upgrade my diff internally and see. Very excited to take in the new features ❤️

@Aghassi
Copy link
Author

Aghassi commented Jun 20, 2023

@JamieMason Just confirming I'm able to take in the latest version without issue. And it has the pnpm fixes too! This is AWESOME. Thank you for all your hard work!

@JamieMason
Copy link
Owner

You're welcome mate, which pnpm fix was it by the way?

@Aghassi
Copy link
Author

Aghassi commented Jun 21, 2023

@JamieMason workspace:* support, that fix :)

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

No branches or pull requests

2 participants