[sdk/nodejs] Prevent Pulumi from overriding tsconfig.json options. (#7068)

This commit is contained in:
PND 2021-08-16 10:58:43 +09:00 committed by GitHub
parent e678b798fe
commit b5ee840b16
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 87 additions and 2 deletions

View file

@ -3,6 +3,9 @@
- [cli] Stop printing secret value on `pulumi config set` if it looks like a secret.
[#7327](https://github.com/pulumi/pulumi/pull/7327)
- [sdk/nodejs] Prevent Pulumi from overriding tsconfig.json options.
[#7068](https://github.com/pulumi/pulumi/pull/7068)
- [sdk/go] - Permit declaring explicit resource dependencies via
`ResourceInput` values.
[#7584](https://github.com/pulumi/pulumi/pull/7584)

View file

@ -16,6 +16,7 @@ import * as fs from "fs";
import * as minimist from "minimist";
import * as path from "path";
import * as tsnode from "ts-node";
import { parseConfigFileTextToJson } from "typescript";
import { ResourceError, RunError } from "../../errors";
import * as log from "../../log";
import * as runtime from "../../runtime";
@ -162,7 +163,17 @@ export function run(opts: RunOpts): Promise<Record<string, any> | undefined> | P
// that the "root" of the project is the cwd, if there's a tsconfig.json file here. Otherwise,
// just tell ts-node to not load project options at all. This helps with cases like
// pulumi/pulumi#1772.
const skipProject = !fs.existsSync("tsconfig.json");
const tsConfigPath = "tsconfig.json";
const skipProject = !fs.existsSync(tsConfigPath);
let compilerOptions: object;
try {
const tsConfigString = fs.readFileSync(tsConfigPath).toString();
const tsConfig = parseConfigFileTextToJson(tsConfigPath, tsConfigString).config;
compilerOptions = tsConfig["compilerOptions"] ?? {};
} catch (e) {
compilerOptions = {};
}
if (opts.typeScript) {
tsnode.register({
@ -173,6 +184,7 @@ export function run(opts: RunOpts): Promise<Record<string, any> | undefined> | P
module: "commonjs",
moduleResolution: "node",
sourceMap: "true",
...compilerOptions,
},
});
}

View file

@ -16,6 +16,7 @@ import * as fs from "fs";
import * as minimist from "minimist";
import * as path from "path";
import * as tsnode from "ts-node";
import { parseConfigFileTextToJson } from "typescript";
import { ResourceError, RunError } from "../../errors";
import * as log from "../../log";
import * as runtime from "../../runtime";
@ -149,7 +150,17 @@ export function run(argv: minimist.ParsedArgs,
// find a tsconfig.json. For us, it's reasonable to say that the "root" of the project is the cwd,
// if there's a tsconfig.json file here. Otherwise, just tell ts-node to not load project options at all.
// This helps with cases like pulumi/pulumi#1772.
const skipProject = !fs.existsSync("tsconfig.json");
const tsConfigPath = "tsconfig.json";
const skipProject = !fs.existsSync(tsConfigPath);
let compilerOptions: object;
try {
const tsConfigString = fs.readFileSync(tsConfigPath).toString();
const tsConfig = parseConfigFileTextToJson(tsConfigPath, tsConfigString).config;
compilerOptions = tsConfig["compilerOptions"] ?? {};
} catch (e) {
compilerOptions = {};
}
if (typeScript) {
tsnode.register({
@ -160,6 +171,7 @@ export function run(argv: minimist.ParsedArgs,
module: "commonjs",
moduleResolution: "node",
sourceMap: "true",
...compilerOptions,
},
});
}

View file

@ -1125,3 +1125,12 @@ func TestConstructNodeErrorApply(t *testing.T) {
integration.ProgramTest(t, opts)
})
}
// Test targeting `es2016` in `tsconfig.json` works.
func TestCompilerOptionsNode(t *testing.T) {
integration.ProgramTest(t, &integration.ProgramTestOptions{
Dir: filepath.Join("nodejs", "compiler_options"),
Dependencies: []string{"@pulumi/pulumi"},
Quick: true,
})
}

View file

@ -0,0 +1,3 @@
/.pulumi/
/bin/
/node_modules/

View file

@ -0,0 +1,3 @@
name: compiler_options
runtime: nodejs
description: A TypeScript program that sets the `target` in `tsconfig.json` to `es2016`.

View file

@ -0,0 +1,17 @@
// Copyright 2016-2021, Pulumi Corporation. All rights reserved.
// Confirm that Pulumi no longer forces the `target` `compilerOptions` in `tsconfig.json` to be `es6`.
//
// When `es6` is the target, if there isn't a version of the `@types/node` package available
// that declares `Array.prototype.includes`, using `includes` will result in a compiler error:
//
// error TS2339: Property 'includes' does not exist on type 'number[]'.
//
// This test includes a resolution to force the use of an earlier version of `@types/node` that
// does not declare `Array.prototype.includes` (because it's a transitive dependency) and
// sets the `target` in `tsconfig.json` to `es2016`, which provides a declaration for `includes`.
// The program should run without any compiler errors.
if ([1, 2, 3].includes(1)) {
console.log("hello world");
}

View file

@ -0,0 +1,11 @@
{
"name": "compiler_options",
"license": "Apache-2.0",
"peerDependencies": {
"@pulumi/pulumi": "^3.0.0"
},
"//": "Force an old version of @types/node that doesn't have a declaration of `Array.prototype.includes`",
"resolutions": {
"@types/node": "4.0.29"
}
}

View file

@ -0,0 +1,15 @@
{
"compilerOptions": {
"strict": true,
"outDir": "bin",
"target": "es2016",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"experimentalDecorators": true,
"pretty": true,
"noFallthroughCasesInSwitch": true,
"noImplicitReturns": true,
"forceConsistentCasingInFileNames": true,
}
}