aws / aws/aws-cdk

aws-lambda-nodejs: Precompilation excludes path aliases in tsc command

Open
#20,666 12 comments 3 reactions 0 assignees View on GitHub
@aws-cdk/aws-lambda-nodejs bug effort/small p2
Dominant language
TypeScript
Stars
12.9k
Forks
4.6k
Avg merge
2d 3h
Merged PRs (30d)
83

Description

### Describe the bug

Given the following compiler options:
```json
{
"lib": ["ESNext"],
"moduleResolution": "node",
"module": "commonjs",
"target": "ES2020",
"baseUrl": ".",
"paths": {
"@core/*": ["./backend/core/*"],
"@framework/*": ["./backend/framework/*"],
"@application/*": ["./backend/application/*"],
"@graphql/*": ["./backend/graphql/*"],
"@graphql/types": ["./backend/graphql/generated/types"],
"@tests/*": ["./backend/tests/*"]
},
"allowSyntheticDefaultImports": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"esModuleInterop": true,
"skipLibCheck": true,
"outDir": "dist",
"rootDir": "./",
"strict": true,
"noImplicitAny": false,
"strictNullChecks": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": true,
"noFallthroughCasesInSwitch": true,
"removeComments": true,
"sourceMap": true,
"strictPropertyInitialization": false
}
```
The resultant compiler options given to the tsc runner excludes the paths object. This results in compiler errors. I've tracked this down to [getTsconfigCompilerOptions()](https://github.com/aws/aws-cdk/blob/5696c5da865c2461121ac12754eb53c5a9024e51/packages/%40aws-cdk/aws-lambda-nodejs/lib/util.ts#L148). The code handles only objects that are arrays.

### Expected Behavior

Compiler options string should be (newlines added for readability)
```
--lib ESNext
--moduleResolution node
--module commonjs
--target ES2020
--baseUrl .
--paths {
"@core/*": ["./backend/core/*"],
"@framework/*": ["./backend/framework/*"],
"@application/*": ["./backend/application/*"],
"@graphql/*": ["./backend/graphql/*"],
"@graphql/types": ["./backend/graphql/generated/types"],
"@tests/*": ["./backend/tests/*"]
}
--allowSyntheticDefaultImports
--experimentalDecorators
--emitDecoratorMetadata
--esModuleInterop
--skipLibCheck
--outDir ./
--rootDir ./
--strict
--noImplicitReturns
--noFallthroughCasesInSwitch
--removeComments
--sourceMap
```

### Current Behavior

Compiler options are (newlines added for readability)
```
--lib ESNext
--moduleResolution node
--module commonjs
--target ES2020
--baseUrl .
--allowSyntheticDefaultImports
--experimentalDecorators
--emitDecoratorMetadata
--esModuleInterop
--skipLibCheck
--outDir ./
--rootDir ./
--strict
--noImplicitReturns
--noFallthroughCasesInSwitch
--removeComments
--sourceMap
```

Which results in `Error: Failed to bundle asset...`

### Reproduction Steps

### tsconfig.json
```json
{
"compilerOptions": {
"lib": ["ESNext"],
"moduleResolution": "node",
"baseUrl": "."
"paths": {
"@utils/*": "./utils/*"
}
}
}
```

### utils/calculator.ts
```typescript
export function add(a: number, b: number): number {
return a + b;
}
```

### index.ts
```typescript
import { add } from '@utils/calculator';
console.log(add(1,2));
```

### Possible Solution

In [getTsconfigCompilerOptions()](https://github.com/aws/aws-cdk/blob/5696c5da865c2461121ac12754eb53c5a9024e51/packages/%40aws-cdk/aws-lambda-nodejs/lib/util.ts#L181-L184), add an else condition to emit the json object as part of the command.

### Additional Information/Context

_No response_

### CDK CLI Version

2.27.0 (build 8e89048)

### Framework Version

_No response_

### Node.js Version

v18.2.0

### OS

Ubuntu 22.04 LTS

### Language

Typescript

### Language Version

Typescript (4.7.2)

### Other information

_No response_

Contributor guide

Open the contributing guide

Research direction

Start in packages/@aws-cdk/aws-lambda-nodejs/lib/util.ts at getTsconfigCompilerOptions(), then compare its handling of the paths object with the tsconfig.json and index.ts reproduction. Done means the generated tsc options preserve the path aliases and the example no longer fails with unresolved imports.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
build-system
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.