break-stuff / cem-tools

Tooling for generating features based on the Custom Elements Manifest
MIT License
98 stars 13 forks source link

bug(jet-brains-integration): error if process.cwd()!==packageRoot, even if not creating the manifest #151

Open maxpatiiuk opened 2 months ago

maxpatiiuk commented 2 months ago

jet-brains-integration is assuming that process.cwd() is pointing to package root. If it is not, it crashes at this line:

https://github.com/break-stuff/cem-tools/blob/940144cb90d42c152506b0fa9be6adf2ac6fb5e4/packages/jet-brains-integration/src/web-types-generator.ts#L35

Since the fs access is in the global scope, it crashes even if the code didn't directly generate the manifest - any indirect imports to jet-brains-integration trigger the error:

❯ yarn s2l:migrate
node:fs:448
    return binding.readFileUtf8(path, stringToFlags(options.flag));
                   ^

Error: ENOENT: no such file or directory, open 'package.json'
    at Object.readFileSync (node:fs:448:20)
    at file://.../node_modules/custom-element-jet-brains-integration/dist/index.js:256:34
    at ModuleJob.run (node:internal/modules/esm/module_job:262:25)
    at async ModuleLoader.import (node:internal/modules/esm/loader:474:24)
    at async asyncRunEntryPointWithESMLoader (node:internal/modules/run_main:109:5) {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: 'package.json'
}

Suggestions:

break-stuff commented 1 month ago

In this scenario, you can set the packageJson option to false and it will skip this step, so you can manually add the property.

maxpatiiuk commented 1 month ago

In this scenario, you can set the packageJson option to false and it will skip this step, so you can manually add the property.

To clarify, jet-brains-integration is doing fs.readFileSync() in the global scope - this is before you get a chance to pass packageJson:false.

https://github.com/break-stuff/cem-tools/blob/940144cb90d42c152506b0fa9be6adf2ac6fb5e4/packages/jet-brains-integration/src/web-types-generator.ts#L35

In my case, the exception was occurring when I wasn't even calling jet-brains-integration directly - it was just imported indirectly as part of another import - I had to restructure my code to avoid htat.