NodeJsFunction: Esbuild PNPM command executed in wrong directory
- Dominant language
- TypeScript
- Stars
- 12.9k
- Forks
- 4.6k
- Avg merge
- 1d 19h
- Merged PRs (30d)
- 71
Description
### Describe the bug
Generally, within a RushJs monorepo using pnpm the root directory features the lock file, sub packages do not.
```
/usr/src/app/pnpm-lock.yml
```
And the projects feature the code
```
/usr/src/app/package/character-sheet/*
```
When code in character-sheet calls NodeJsFunction goes to run esbuild and runs it in directory /usr/src/app/ which exits and fails.
```
Error: Failed to bundle asset CharacterSheetStack/character-sheet-stack-nestjs/nodejsFunction/Code/Stage, bundle output is located at /usr/src/app/packages/character-sheet/cdk.out/bundling-temp-9d434c76abca8834afb013be867bec892c6375518c2729ca2870bd4f1b049761-error: Error: bash -c pnpm exec -- tsc "/usr/src/app/packages/character-sheet/src/index.ts" --allowJs --allowSyntheticDefaultImports --allowUnreachableCode false --alwaysStrict --declaration --emitDecoratorMetadata --esModuleInterop --experimentalDecorators --incremental false --inlineSourceMap --inlineSources --lib es2021 --module commonjs --moduleResolution node --noFallthroughCasesInSwitch false --noImplicitAny --noImplicitReturns --noImplicitThis --noUnusedLocals false --noUnusedParameters false --outDir ./ --resolveJsonModule --rootDir ./ --strict --strictNullChecks --strictPropertyInitialization false --target es2021 --typeRoots ../../node_modules/@types && pnpm exec -- esbuild --bundle "/usr/src/app/packages/character-sheet/src/index.js" --target=es2021 --platform=node --outfile="/usr/src/app/packages/character-sheet/cdk.out/bundling-temp-9d434c76abca8834afb013be867bec892c6375518c2729ca2870bd4f1b049761/index.js" --sourcemap --external:express --external:reflect-metadata --external:rxjs --external:source-map-support --external:aws-lambda --external:class-transformer --external:class-validator --external:@nestjs/common --external:@nestjs/core --external:@nestjs/terminus --external:@vendia/serverless-express --external:express --external:node-cache --external:reflect-metadata --external:rxjs --keep-names run in directory /usr/src/app exited with status 1
```
I'd like to run the commands with the cwd /usr/src/app/package/character-sheet. Also it seems to error when adjusting the cwd, etc. to run it from the package directory because the lock file is not within it. https://github.com/aws/aws-cdk/blob/cef98cf20357bb0f3748e315e4bea093a97e2302/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L249
If I take the command that errors, such as the one above, and run it from within the correct directory it works.
### Expected Behavior
NodeJSFunction bundles packages.
### Current Behavior
exit 1
pnpm exec -- esbuild is ran from /usr/src/app where it doesn't exist and fails. There doesn't seem to be any options to correctly set :?
### Reproduction Steps
There is a CodeSpace here that should work to duplicate the issue using this commit https://github.com/hxtree/cats-cradle/tree/bb3ff9dc1c68f803309a525e879a5948d3a6917d . It's kind of complex as rushjs setup and pnpm is required.
### Possible Solution
Perhaps lock file from parent directory is used by pnpm exec command is ran from within the sub package directory.
https://github.com/aws/aws-cdk/blame/main/packages/%40aws-cdk/aws-lambda-nodejs/lib/bundling.ts#L300
### Additional Information/Context
Although it is entirely possible something else caused this to stop working it seems like a regression bug.
### CDK CLI Version
2.63.1 (build 1d7aff5)
### Framework Version
_No response_
### Node.js Version
16
### OS
linux
### Language
Typescript
### Language Version
Typescript
### Other information
_No response_
Contributor guide
Research direction
Start with packages/@aws-cdk/aws-lambda-nodejs/lib/bundling.ts, especially the command and working-directory handling referenced in the issue. Reproduce the failure with the linked cats-cradle commit using its RushJs and pnpm setup, then compare execution from the repository root and the character-sheet package directory. Done means NodeJsFunction bundles successfully in that monorepo setup while retaining access to the parent lock file.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- node.js, typescript
- Domain
- build-system
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100