Skip to content

jsii fails with bad error if outdir is under projectRoot #2060

@rix0rrr

Description

@rix0rrr

With the following setup:

# /path/to/projectDir
# package.json 
{
  "name": "teststatic",
  "main": "index.js",
  "types": "index.d.ts",
  "jsii": {
    "versionFormat": "full",
    "targets": {
      "dotnet": {
        "namespace": "Bla.Module",
        "packageId": "Bla.Module"
      }
    },
    "tsc": {
      "outDir": "dist"
    }
  },
}

So: we compile to an outdir dist underneath the project directory, if we then try to compile then jsii errors:

error JSII4: Could not find "main" file: /path/to/index.ts

It looks in the wrong directory for the entry point file.

The reason is this code (in JS form):

        const tscOutDir = (0, helpers_1.normalizeConfigPath)(projectInfo.projectRoot, program.getCompilerOptions().outDir);
        if (tscOutDir != null) {
            mainFile = path.relative(tscOutDir, mainFile);
            // rootDir may be set explicitly or not. If not, inferRootDir replicates
            // tsc's behavior of using the longest prefix of all built source files.
            this.tscRootDir = program.getCompilerOptions().rootDir ?? inferRootDir(program);
            if (this.tscRootDir != null) {
                mainFile = path.join(this.tscRootDir, mainFile);
            }
        }
        this.mainFile = path.resolve(projectInfo.projectRoot, mainFile);

Now mainFile contains ../index.ts (outDir to mainFile), but it will be combined with projectRoot to come up with a file in the wrong directory.

What's worse, jsii-config recommended this setup with the dist directory to me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions