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

Translation to angular.json Does Not Replace Variables e.g. {projectRoot} #29052

Open
1 of 4 tasks
Char2sGu opened this issue Nov 24, 2024 · 0 comments
Open
1 of 4 tasks

Comments

@Char2sGu
Copy link

Current Behavior

When migrating Angular applications, most migrations fail because they could not locate project tsconfigs.
Image

I hooked into the internal getProjectTsConfigPaths.js in @angular/core/schematics/bundles/project_tsconfig_paths-<hash>.js and logged the accepted workspace configuration to the console, observing that all the {projectRoot} and {projectName} variables are left as-is, without interpolation, causing tsconfigs not found:

/**
 * Gets all tsconfig paths from a CLI project by reading the workspace configuration
 * and looking for common tsconfig locations.
 */
async function getProjectTsConfigPaths(tree) {
    // Start with some tsconfig paths that are generally used within CLI projects. Note
    // that we are not interested in IDE-specific tsconfig files (e.g. /tsconfig.json)
    const buildPaths = new Set();
    const testPaths = new Set();
    const workspace = await getWorkspace(tree);
////////////////////////////////////////////////////////////////
// inserts logging here
    console.log(workspace);
////////////////////////////////////////////////////////////////
    for (const [, project] of workspace.projects) {
        for (const [name, target] of project.targets) {
            if (name !== 'build' && name !== 'test') {
                continue;
            }
            for (const [, options] of allTargetOptions(target)) {
                const tsConfig = options['tsConfig'];
                // Filter out tsconfig files that don't exist in the CLI project.
////////////////////////////////////////////////////////////////
// received tsConfig is "{projectRoot}/tsconfig.app.json"
// tree.exists(tsConfig) returns false, leading to skipping this project
                if (typeof tsConfig !== 'string' || !tree.exists(tsConfig)) {
////////////////////////////////////////////////////////////////
                    continue;
                }
                if (name === 'build') {
                    buildPaths.add(core.normalize(tsConfig));
                }
                else {
                    testPaths.add(core.normalize(tsConfig));
                }
            }
        }
    }
    return {
        buildPaths: [...buildPaths],
        testPaths: [...testPaths],
    };
}

Original project.json:

Image

Translated angular.json:

Image

Expected Behavior

When translating to angular.json, the variables should be replaced with values.

GitHub Repo

No response

Steps to Reproduce

  1. initialize nx within an Angular v18 workspace
  2. nx migrate @angular/core
  3. npm install
  4. nx migrate --run-migrations

Nx Report

Node           : 22.3.0
OS             : linux-x64
Native Target  : x86_64-linux
npm            : 10.8.1

nx (global)    : 20.0.1
nx             : 20.1.3
@nx/js         : 20.1.3
@nx/eslint     : 20.1.3
@nx/workspace  : 20.1.3
@nx/angular    : 20.1.3
@nx/devkit     : 20.1.3
@nx/web        : 20.1.3
@nx/webpack    : 20.1.3
typescript     : 5.5.4
---------------------------------------
Community plugins:
@ng-doc/builder : 17.6.16
ng-apexcharts   : 1.13.0
---------------------------------------
Local workspace plugins:
         @workspace/build

Failure Logs

NX   Running migrations from 'migrations.json'

Running the following migrations:
- @angular/core: explicit-standalone-flag (Updates non-standalone Directives, Component and Pipes to 'standalone:false' and removes 'standalone:true' from those who are standalone)
- @angular/core: pending-tasks (Updates ExperimentalPendingTasks to PendingTasks)
- @angular/core: provide-initializer (Replaces `APP_INITIALIZER`, `ENVIRONMENT_INITIALIZER` & `PLATFORM_INITIALIZER` respectively with `provideAppInitializer`, `provideEnvironmentInitializer` & `providePlatformInitializer`.)
---------------------------------------------------------

Running migration @angular/core: explicit-standalone-flag

 NX   Failed to run explicit-standalone-flag from @angular/core. This workspace is NOT up to date!


 NX   Could not find any tsconfig file. Cannot run the explicit-standalone-flag migration.

Error: Could not find any tsconfig file. Cannot run the explicit-standalone-flag migration.
    at /home/char2s/projects/workspace/node_modules/@angular/core/schematics/bundles/explicit-standalone-flag.js:153:19
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async callRuleAsync (/home/char2s/projects/workspace/node_modules/@angular-devkit/schematics/src/rules/call.js:77:18)

nx migrate [packageAndVersion]

Creates a migrations file or runs migrations from the migrations file.
- Migrate packages and create migrations.json (e.g., nx migrate @nx/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json). Use flag --if-exists to run migrations only if the migrations file exists.

Positionals:
  packageAndVersion  The target package and version (e.g, @nx/[email protected]).                                                                                            [string]

Options:
      --help                      Show help                                                                                                                                [boolean]
      --version                   Show version number                                                                                                                      [boolean]
      --verbose                   Prints additional information about the commands (e.g., stack traces).                                                                   [boolean]
      --runMigrations             Execute migrations from a file (when the file isn't provided, execute migrations from migrations.json).                                   [string]
      --ifExists                  Run migrations only if the migrations file exists, if not continues successfully.                                       [boolean] [default: false]
      --from                      Use the provided versions for packages instead of the ones installed in node_modules (e.g., --from="@nx/[email protected],@nx/[email protected]").     [string]
      --to                        Use the provided versions for packages instead of the ones calculated by the migrator (e.g., --to="@nx/[email protected],@nx/[email protected]").      [string]
  -C, --createCommits             Automatically create a git commit after each migration runs.                                                            [boolean] [default: false]
      --commitPrefix              Commit prefix to apply to the commit for each migration, when --create-commits is enabled.            [string] [default: "chore: [nx migration] "]
      --interactive               Enable prompts to confirm whether to collect optional package updates and migrations.                                   [boolean] [default: false]
      --excludeAppliedMigrations  Exclude migrations that should have been applied on previous updates. To be used with --from.                           [boolean] [default: false]

Examples:
  migrate latest                                                           Update all Nx plugins to "latest". This will generate migrations.json
  migrate 9.0.0                                                            Update all Nx plugins to "9.0.0". This will generate migrations.json
  migrate @nx/[email protected] --from="@nx/[email protected],@nx/[email protected]"  Update @nx/workspace and generate the list of migrations starting with version 8.0.0 of @nx/workspace and
                                                                           @nx/node, regardless of what is installed locally
  migrate @nx/[email protected] --to="@nx/[email protected],@nx/[email protected]"     Update @nx/workspace to "9.0.0". If it tries to update @nx/react or @nx/angular, use version "9.0.1"
  migrate [email protected]                                           Update another-package to "12.0.0". This will update other packages and will generate migrations.json
                                                                           file
  migrate latest --interactive                                             Collect package updates and migrations in interactive mode. In this mode, the user will be prompted
                                                                           whether to apply any optional package update and migration
  migrate latest [email protected] --exclude-applied-migrations             Collect package updates and migrations starting with version 14.5.0 of "nx" (and Nx first-party plugins),
                                                                           regardless of what is installed locally, while excluding migrations that should have been applied on
                                                                           previous updates
  migrate --run-migrations=migrations.json                                 Run migrations from the provided migrations.json file. You can modify migrations.json and run this
                                                                           command many times
  migrate --run-migrations --create-commits                                Create a dedicated commit for each successfully completed migration. You can customize the prefix used
                                                                           for each commit by additionally setting --commit-prefix="PREFIX_HERE "

Find more information and examples at https://nx.dev/nx/migrate

Error: Command failed: npx nx _migrate --run-migrations --verbose
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:889:11)
    at execSync (node:child_process:961:15)
    at runNxSync (/home/char2s/projects/workspace/node_modules/nx/src/utils/child-process.js:28:34)
    at runLocalMigrate (/home/char2s/projects/workspace/node_modules/nx/src/command-line/migrate/migrate.js:1057:39)
    at Object.runMigration (/home/char2s/projects/workspace/node_modules/nx/src/command-line/migrate/migrate.js:1080:9)
    at Object.handler (/home/char2s/projects/workspace/node_modules/nx/src/command-line/migrate/command-object.js:13:68) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 75917,
  stdout: null,
  stderr: null
}

 NX   Command failed: npx nx migrate --run-migrations --verbose

Error: Command failed: npx nx migrate --run-migrations --verbose
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:889:11)
    at execSync (node:child_process:961:15)
    at runNxSync (/tmp/tmp-75247-5r82fFPh8Tsq/node_modules/nx/src/utils/child-process.js:28:34)
    at runMigrations (/tmp/tmp-75247-5r82fFPh8Tsq/node_modules/nx/src/command-line/migrate/migrate.js:985:39)
    at /tmp/tmp-75247-5r82fFPh8Tsq/node_modules/nx/src/command-line/migrate/migrate.js:1051:19
    at async handleErrors (/tmp/tmp-75247-5r82fFPh8Tsq/node_modules/nx/src/utils/handle-errors.js:8:24)

nx migrate [packageAndVersion]

Creates a migrations file or runs migrations from the migrations file.
- Migrate packages and create migrations.json (e.g., nx migrate @nx/workspace@latest)
- Run migrations (e.g., nx migrate --run-migrations=migrations.json). Use flag --if-exists to run migrations only if the migrations file exists.

Positionals:
  packageAndVersion  The target package and version (e.g, @nx/[email protected]).                                                                                            [string]

Options:
      --help                      Show help                                                                                                                                [boolean]
      --version                   Show version number                                                                                                                      [boolean]
      --verbose                   Prints additional information about the commands (e.g., stack traces).                                                                   [boolean]
      --runMigrations             Execute migrations from a file (when the file isn't provided, execute migrations from migrations.json).                                   [string]
      --ifExists                  Run migrations only if the migrations file exists, if not continues successfully.                                       [boolean] [default: false]
      --from                      Use the provided versions for packages instead of the ones installed in node_modules (e.g., --from="@nx/[email protected],@nx/[email protected]").     [string]
      --to                        Use the provided versions for packages instead of the ones calculated by the migrator (e.g., --to="@nx/[email protected],@nx/[email protected]").      [string]
  -C, --createCommits             Automatically create a git commit after each migration runs.                                                            [boolean] [default: false]
      --commitPrefix              Commit prefix to apply to the commit for each migration, when --create-commits is enabled.            [string] [default: "chore: [nx migration] "]
      --interactive               Enable prompts to confirm whether to collect optional package updates and migrations.                                   [boolean] [default: false]
      --excludeAppliedMigrations  Exclude migrations that should have been applied on previous updates. To be used with --from.                           [boolean] [default: false]

Examples:
  migrate latest                                                           Update all Nx plugins to "latest". This will generate migrations.json
  migrate 9.0.0                                                            Update all Nx plugins to "9.0.0". This will generate migrations.json
  migrate @nx/[email protected] --from="@nx/[email protected],@nx/[email protected]"  Update @nx/workspace and generate the list of migrations starting with version 8.0.0 of @nx/workspace and
                                                                           @nx/node, regardless of what is installed locally
  migrate @nx/[email protected] --to="@nx/[email protected],@nx/[email protected]"     Update @nx/workspace to "9.0.0". If it tries to update @nx/react or @nx/angular, use version "9.0.1"
  migrate [email protected]                                           Update another-package to "12.0.0". This will update other packages and will generate migrations.json
                                                                           file
  migrate latest --interactive                                             Collect package updates and migrations in interactive mode. In this mode, the user will be prompted
                                                                           whether to apply any optional package update and migration
  migrate latest [email protected] --exclude-applied-migrations             Collect package updates and migrations starting with version 14.5.0 of "nx" (and Nx first-party plugins),
                                                                           regardless of what is installed locally, while excluding migrations that should have been applied on
                                                                           previous updates
  migrate --run-migrations=migrations.json                                 Run migrations from the provided migrations.json file. You can modify migrations.json and run this
                                                                           command many times
  migrate --run-migrations --create-commits                                Create a dedicated commit for each successfully completed migration. You can customize the prefix used
                                                                           for each commit by additionally setting --commit-prefix="PREFIX_HERE "

Find more information and examples at https://nx.dev/nx/migrate

Error: Command failed: /tmp/tmp-75247-5r82fFPh8Tsq/node_modules/.bin/nx _migrate --run-migrations --verbose
    at genericNodeError (node:internal/errors:983:15)
    at wrappedFn (node:internal/errors:537:14)
    at checkExecSyncError (node:child_process:889:11)
    at execSync (node:child_process:961:15)
    at Object.runMigration (/home/char2s/projects/workspace/node_modules/nx/src/command-line/migrate/migrate.js:1073:42)
    at Object.handler (/home/char2s/projects/workspace/node_modules/nx/src/command-line/migrate/command-object.js:13:68) {
  status: 1,
  signal: null,
  output: [ null, null, null ],
  pid: 75457,
  stdout: null,
  stderr: null
}

Package Manager Version

No response

Operating System

  • macOS
  • Linux
  • Windows
  • Other (Please specify)

Additional Information

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant