Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cannot build package with project references #260

Closed
dagda1 opened this issue Jan 21, 2021 · 4 comments
Closed

cannot build package with project references #260

dagda1 opened this issue Jan 21, 2021 · 4 comments
Labels
kind: bug Something isn't working properly problem: no repro No reproduction was provided (and have not tried to repro without one) scope: upstream Issue in upstream dependency solution: workaround available There is a workaround available for this issue topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc)

Comments

@dagda1
Copy link

dagda1 commented Jan 21, 2021

What happens and why it is wrong

I am trying to build in a package that has project references in the tsconfig.json

{
  "extends": "../../tsconfig-base.json",
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src" 
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"],
  "references": [
    { "path": "../util" }
  ]
}

The project builds fine if I run yarn run tsc --build but fails when using rollup and the rollup-plugin-typescript2.

I can see in the source that if the tsconfig has project references then files from the references are included:

    if (parsedConfig.projectReferences) {
      included = lodash.concat(included, expandIncludeWithDirs(included, parsedConfig.projectReferences.map((x) => x.path)));
      excluded = lodash.concat(excluded, expandIncludeWithDirs(excluded, parsedConfig.projectReferences.map((x) => x.path)));
    }

Which will create an array of globs like this:

[
  '*.ts+(|x)',
  '**/*.ts+(|x)',
  '/Users/paulcowan/projects/cuttingedge/packages/util/*.ts+(|x)',
  '/Users/paulcowan/projects/cuttingedge/packages/util/**/*.ts+(|x)'
]

I then get this error:

: semantic error TS6059: File '/Users/paulcowan/projects/cuttingedge/packages/util/src/index.ts' is not under 'rootDir' '/Users/paulcowan/projects/cuttingedge/packages/hooks'. 'rootDir' is expected to contain all source files.

Reading that error, I would then need to update my tsconfig.json to this:

{
  "extends": "../../tsconfig-base.json",
  "compilerOptions": {
    "outDir": "dist",
    "rootDirs": ["./src", "../util" ]
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["./dist", "src/**/*.test.ts", "src/**/*.test.tsx"],
  "references": [
    { "path": "../util" }
  ]
}

I ten get this error:

Unexpected token (Note that you need plugins to import files that are not JavaScript)

Looking at the source, I don't see anywhere that takes into account a project references build apart from the filter code I pasted at the start.

Versions
typescript: 4.1.3
rollup: 2.37.0
rollup-plugin-typesscript2: 0.29.0

rollup.config.js

`rollup.config.js`:
{
    input: inputFile,
    external: (id: string) => {
      if (id === 'babel-plugin-transform-async-to-promises/helpers') {
        return false;
      }

      return !id.startsWith('.') && !path.isAbsolute(id);
    },
    treeshake: {
      moduleSideEffects: false,
    },
    plugins: [
      eslint({
        fix: false,
        throwOnError: true,
        throwOnWarning: true,
        extensions: ['.ts', '.tsx', '.test.ts', '.test.tsx'],
        filterInclude: 'src/**',
        filterExclude: ['**/*.scss', '**/*.css', '**/*.md', '**/*.csv', 'dist/**', '**/*.json'],
        useEslintrc: true,
      }),
      resolve({
        mainFields: ['module', 'browser', 'main'],
        extensions: ['.mjs', '.cjs', '.js', '.ts', '.tsx', '.json', '.jsx'],
      }),
      json(),
      md(),
      postcss({
        extract: true,
        modules: false,
        autoModules: true,
        sourceMap: true,
        use: ['sass'],
        plugins: [
          url({
            url: 'inline',
          }),
        ],
      }),
      csv(),
      typescript({
        clean: true,
        typescript: require('typescript'),
        tsconfig: paths.tsConfig,
        abortOnError: true,
        tsconfigDefaults: {
          compilerOptions: {
            sourceMap: true,
            declaration: true,
            target: 'esnext',
            jsx: 'react-jsx',
          },
          useTsconfigDeclarationDir: true,
        },
        tsconfigOverride: {
          compilerOptions: {
            sourceMap: true,
            target: 'esnext',
          },
        },
      }),
      babel({
        exclude: /\/node_modules\/(core-js)\//,
        babelHelpers: 'runtime',
        ...babelConfig,
      } as RollupBabelInputPluginOptions),
      injectProcessEnv({
        NODE_ENV: env,
      }),
      svgo(),
      sourceMaps(),
      minify &&
        terser({
          compress: {
            keep_infinity: true,
            pure_getters: true,
            passes: 10,
          },
          ecma: 2016,
          toplevel: moduleFormat === 'cjs',
          format: {
            comments: 'all',
          },
        }),
    ].filter(Boolean),
  });
}

tsconfig.json

`tsconfig.json`:
{
  "extends": "../../tsconfig-base.json",
  "compilerOptions": {
    "outDir": "dist",
    "rootDir": "./src" 
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"],
  "references": [
    { "path": "../util" }
  ]
}

plugin output with verbosity 3

plugin output with verbosity 3:
@dagda1 dagda1 changed the title can build package with project references cannot build package with project references Jan 21, 2021
@ezolenko
Copy link
Owner

Did you try not using rootDir in tsconfig at all?

@dagda1
Copy link
Author

dagda1 commented Jan 21, 2021

@ezolenko adding "baseUrl" to each of the monorepo tsconfig's fixed it:

{
  "extends": "../../tsconfig-base.json",
  "compilerOptions": {
    "baseUrl": ".",
    "outDir": "./dist",
    "rootDir": "./src",
  },
  "include": ["src/**/*.ts", "src/**/*.tsx"],
  "exclude": ["src/**/*.test.ts", "src/**/*.test.tsx"],
  "references": [
    { "path": "../util" }
  ]
}

I am both relieved and surprised!

@dagda1 dagda1 closed this as completed Jan 21, 2021
@agilgur5 agilgur5 added topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc) solution: workaround available There is a workaround available for this issue labels May 8, 2022
@agilgur5
Copy link
Collaborator

So I tried the baseUrl workaround in #295 (comment) (see "attempted to workaround" section) and it didn't change anything for me there.

Per that comment, I suspect this might be due to how extends works confusingly with relative paths (i.e. microsoft/TypeScript#29172).
Looking at this issue again though, it does say that tsc works though, so maybe it's not related to the pathing of extends... 🤔

Hard to tell or do a root cause analysis without a minimal repro, especially as the base config isn't even provided here.
Looking at the Rollup config, this also looks like it potentially uses an old config from TSDX (I solo-maintained TSDX for ~1.5 years, so parts of that are written by me and hence very recognizable), and then adds quite a bit more config on top of that, so there's a lot of potential confounding factors in this particular case.

@agilgur5 agilgur5 added kind: bug Something isn't working properly scope: upstream Issue in upstream dependency problem: no repro No reproduction was provided (and have not tried to repro without one) labels May 27, 2022
@agilgur5
Copy link
Collaborator

agilgur5 commented May 27, 2022

After investigating in #295 (comment) a bit more, the reason project references failed here may have been due to an upstream issue with @rollup/pluginutils.

rollup/plugins#517 looks to fix absolute paths. And TS parses out references's paths as absolute paths (it's possible that baseUrl changes that? don't think it does).

rpt2 v0.30+ upgraded @rollup/pluginutils to a version that includes that PR, so a newer rpt2 should fix this issue (OP was using rpt2 v0.29.0, and 0.30.0 was released a ~month after this was opened).

Can't quite confirm that without a repro though, and one isn't provided here, but thought I'd at least give my best guess hypothesis as of right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: bug Something isn't working properly problem: no repro No reproduction was provided (and have not tried to repro without one) scope: upstream Issue in upstream dependency solution: workaround available There is a workaround available for this issue topic: monorepo / symlinks Related to monorepos and/or symlinks (Lerna, Yarn, PNPM, Rush, etc)
Projects
None yet
Development

No branches or pull requests

3 participants