`pulumi` cli does not respect the configurations I...
# general
i
pulumi
cli does not respect the configurations I place in the tsconfig.json - which really impacts code reuse. Who can chat with me about the internals of the cli?
Following the https://github.com/pulumi/kubernetes-the-prod-way, I have multiple stacks. I’m a well versed typescript user and that is one of the draws to the pulumi stack. In trying to reuse some code, as well as change and put
src
elsewhere, I have quickly found out that the
pulumi
cli does not respect the tsconfig.
is it executing ts-node? executing tsc then executing node?
g
@white-balloon-205?
i
I can provide a sample if needed, but I’m simply trying to reuse code with
paths
in the tsconfig
compilerOptions
but any execution of
pulumi up
fails. If I know a bit more about how this works I can probably work it out
m
@bitter-oil-46081, do we have anything written down about using the TypeScript compiler manually? I know it’s possible.
i
from a search on the repo it looks like it is using ts-node, which in my experience works with tsconfig
paths
, but no joy here.
not quite sure what I’m looking at here but trying to help myself 🙂 https://github.com/pulumi/pulumi/blob/master/sdk/nodejs/cmd/pulumi-language-nodejs/main.go#L76
m
I could be wrong, but my understanding is that since we set these, ts-node won’t pick up the
compilerOptions
specified in tsconfig.json….
However, it’s possible to still setup a system where you manually run
tsc
to generate JavaScript, and just have Pulumi run the generated JavaScript file.
i
I want to use ts-node, but it is ignoring my path mapping options in tsconfig
My experience is that pulumi doesn’t read it at all. Proven by doing something as simple as changing the
include
to use a
src
directory instead of at the root
I use ts-node cmd line, I’ve never used it via the api like the pulumi cli does.
b
@important-leather-28796 is the tsconfig.json located next to your package.json or is it in a different place?
i
root, sibling to package.json
I’ll try a ts-node cmdline test
b
Yeah, surprising, I would have expected it to get loaded, based on the code here: https://github.com/pulumi/pulumi/blob/master/sdk/nodejs/cmd/run/run.ts#L135-L148
i
I’ve got the same error with ts-node cli so that proves a problem with my config. Thanks, it is helpful to know how that is working which informs how I should be able to use it. If I get it running with ts-node cli and not with pulumi I’ll ping again
b
Thanks! Full disclosure, I could very much imagine that we are invoking this function "wrong" in some subtle way that is causing undesired behavior.
👍 1
i
Confirmed tsconfig to be working correctly. I am successfully using path aliasing etc.
👍 1
m
@important-leather-28796 I've been struggling to get this same thing to work, do you have an example you can share by chance?
i
It’s internal. I use yarn workspaces. All pulumi stacks under the
cloud
dir. I have shared code in a
pulumi
folder with a package.json name of
@acme/pulumi
.
the other pulumi stacks package.json reference them with dependency
"@acme/pulumi": "link:../pulumi",
m
i
Each stack tsconfig extends a common one, and it uses paths such as:
Copy code
"compilerOptions": {
    "baseUrl": ".",
    "paths": {
      "@acme/pulumi": ["./cloud/pulumi/src"],
    }
  },
there is no need for tsconfig-paths
I stuck a sample file in the shared code dir
cloud/pulumi/src
such as
test.ts
with
export const test = () => 'foo'
then a
testIt.ts
in the stack dir and just attempt to run it with
ts-node ./testIt.ts
once that works, you know you have your tsconfig/package.json’s setup
note that yarn uses
link:
while npm uses
file:
- the package manager semantics matter here
since pulumi doesn’t register
tsconfig-paths
you probably want to get tested/setup to run without it
m
Hmm, it just seems like
paths
from
tsconfig.json
is not getting picked up for me at all. I'm just trying to add a simple alias to get rid of relative imports, no workspaces or anything
Copy code
"compilerOptions": {
    "baseUrl": "./src",
    "paths": {
      "Modules/*": ["modules/*"]
    }
  },
i
it is picked up like a compiled node module, you will need to tsc your shared code dir and make sure the
main
is set in the package.json
my shared code `package.json`:
Copy code
{
  "name": "@acme/pulumi",
  "main": "lib/cjs/index.js",
  "typings": "src/index.ts",
  "license": "UNLICENSED",
  "scripts": {
    "build": "../../node_modules/.bin/tsc"
  },
  "dependencies": {
    "@pulumi/gcp": "^0.17.0",
    "@pulumi/kubernetes": "^0.21.0",
    "@pulumi/pulumi": "^0.17.1",
    "@pulumi/random": "^0.5.0",
  }
}
my shared code package
@acme/pulumi
tsconfig.json
is effectively:
Copy code
{
  "compilerOptions": {
    "module": "commonjs",
    "target": "es2015",
    "jsx": "react",
    "lib": ["es2017", "esnext.asynciterable", "dom"],

    "allowSyntheticDefaultImports": true,
    "declaration": true,
    "declarationMap": true,
    "esModuleInterop": true,
    "forceConsistentCasingInFileNames": false,
    "moduleResolution": "node",
    "noImplicitAny": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noEmitOnError": true,
    "noUnusedLocals": false,
    "strict": true,
    "sourceMap": true,
    "suppressImplicitAnyIndexErrors": true

    "outDir": "lib/cjs",
    "rootDir": "src"
  },
  "include": ["../../../js/typings/**/*", "../../typings/**/*", "./src/**/*"],
  "exclude": ["node_modules", "./lib"]
}
m
My setup is simpler, no yarn workspaces or shared packages or anything. Just a
tsconfig.json
in the root of normal pulumi typescript bootstrap, and hoping to be able to set
paths
to simplify some imports
I wouldn't think I would want to be setting any
build
in
package.json
in my case.
i
ts-node as pulumi uses it does not register tsconfig-paths
so you cannot rely on them for executing the
pulumi *
commands
m
Oh I see, you were not able to this to work directly with the
pulumi
cli?
i
you only have to
build
shared code, not a stack
m
I was just hoping to have my typescript 'path' aliases in my stack code
This may be a different problem than the one you solved
i
pulumi will use your tsconfig, but ts-node does not use
paths
you have to
-r tsconfig-paths
to use
paths
with ts-node, so you will need to figure out how to make pulumi’s use of ts-node via api register
tsconfig-paths
m
Yeah, all makes sense now. I see you were doing something different, and pulumi would need some modification probably to make my use case work.
Thanks for the help.
Actually got
tsconfig-paths
to work with no modification to pulumi
i
how @millions-judge-24978?
register with an env var?
m
This is my entrypoint file which does the trick:
Copy code
// This file is a hack so we can keep pulumi happy
import * as tsConfigPaths from 'tsconfig-paths';

const baseUrl = './src';
const cleanup = tsConfigPaths.register({
  baseUrl,
  paths: {
    'Modules/*': ['modules/*']
  },
});

import main from './src'; // eslint-disable-line

(async () => {
  await main();
  // When path registration is no longer needed
  cleanup();
})();
👍 1