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

fix(amplify-cli-core): use build script properly for overrides #14093

Merged
merged 4 commits into from
Feb 3, 2025

Conversation

brianlenz
Copy link
Contributor

Updated the TypeScript compilation of overrides so that it doesn't require node_modules/.bin/tsc. Instead, it simply relies on the build script to execute tsc. This is more flexible and can support alternative setups w/ hoisting (e.g. via Yarn workspaces).

This is an override corollary fix to #11854, which is for custom resources.

This is an improvement over my previous PR in #13858 in that it works with any package manager by ensuring the --project and tsconfig.json files are passed through to the tsc script. The previous implementation didn't work with npm because it doesn't pass through additional args like yarn does. The fix was easy: simply separate the build run with -- so that the remaining args are treated as positional for the tsc script.

I've confirmed the fix works with both yarn and npm 💪

Description of changes

Remove hard-coded dependency on node_modules/.bin/tsc for overrides to instead use the build script from package.json, which is more flexible.

Issue #11889

Description of how you validated changes

Ran yarn test and all pre-commit hooks without issue.
Tested amplify build --debug and amplify push locally with both yarn and npm. The commands were failing with yarn prior to these changes, and it succeeds with these changes in place (via amplify-dev).

Checklist

  • PR description included
  • yarn test passes

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Updated the TypeScript compilation of overrides so that it doesn't require `node_modules/.bin/tsc`.
Instead, it simply relies on the `build` script to execute `tsc`.
This is more flexible and can support alternative setups w/ hoisting (e.g. via Yarn workspaces).

This is an override corollary fix to #11854, which is for custom resources.

This is an improvement over my previous PR in #13858 in that it works with any package manager
by ensuring the `--project` and `tsconfig.json` files are passed through to the `tsc` script.
The previous implementation didn't work with `npm` because it doesn't pass through additional
args like `yarn` does. The fix was easy: simply separate the build run with `--` so that the
remaining args are treated as positional for the `tsc` script.

I've confirmed the fix works with both `yarn` and `npm` 💪

#11889
@brianlenz
Copy link
Contributor Author

@sobolk it's working now with npm! Just a slight tweak to my last PR needed (using -- to pass through the args to tsc). Tested and confirmed to be working.

It's worth noting that #11854 does not have the same issue because it doesn't pass a custom --project arg. As such, #11854 should be safe to merge as-is for consistency.

If there's anything else I can help with, please let me know!

Thank you 🙏

Tweaked the implementation slightly so that the `--` arg is only
passed for `npm` since it doesn't actually work with `yarn`.

#11889
@sobolk
Copy link
Member

sobolk commented Feb 3, 2025

@sobolk it's working now with npm! Just a slight tweak to my last PR needed (using -- to pass through the args to tsc). Tested and confirmed to be working.

It's worth noting that #11854 does not have the same issue because it doesn't pass a custom --project arg. As such, #11854 should be safe to merge as-is for consistency.

If there's anything else I can help with, please let me know!

Thank you 🙏

@brianlenz I have slightly different solution in mind for this. Where we do not have dependency on build script but we do use package manager to locate tsc.

Package managers have a way to run executables (.bin scripts) from npm packages. And they have a logic to locate the executable after installation (i.e. package manager knows where it is, instead of amplify cli trying to do this).
For example:

npx tsc <args> // note different first token here
yarn tsc <args>
pnpm tsc <args>

Therefore what I would suggest is to

  1. Add packageManager.runner to .
  2. Have sub-classes provide npx, yarn or pnpm for that new property.
  3. Use packageManager.runner when calling tsc (see examples above).
  4. Test solution with npm yarn and pnpm.

@brianlenz
Copy link
Contributor Author

@sobolk thanks for sticking with me and the recommendations here 🙏 I like your suggestions, so I'll take a crack at it!

Added a new `runner` field to the PackageManager interface.
The key difference here is npm's runner is actually `npx`,
whereas yarn and pnpm just use the same executable as the runner.

Updated both the override and custom-resources to use the runner
to run tsc now instead of only looking in `node_modules`.

#11889
@brianlenz
Copy link
Contributor Author

@sobolk done! Check it out and let me know if you think it needs further tweaking 🙏 Tested with npm, yarn, and pnpm. Works great and seems like a much cleaner solution ✨

@sobolk
Copy link
Member

sobolk commented Feb 3, 2025

@sobolk done! Check it out and let me know if you think it needs further tweaking 🙏 Tested with npm, yarn, and pnpm. Works great and seems like a much cleaner solution ✨

I'll test it on my end and I'll get back to you.

@sobolk sobolk merged commit aab715d into aws-amplify:dev Feb 3, 2025
6 checks passed
@brianlenz brianlenz deleted the fix_override_tsc_script_again branch February 10, 2025 20:25
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

Successfully merging this pull request may close these issues.

3 participants