question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce path variables to project.json files

See original GitHub issue

Description

Introduce 2 variables to be used in projects.json files:

  • One with the absolute path to the monorepo directory;
  • Another one with the absolute path to the project directory.

Motivation

The introduction of project.json files in v12 (and the ability to extract project configurations from workspace.json to them) was a great improvement. Keeping configuration close to the project it concerns is great. But I feel that those files (project.json) should be unaware of both the monorepo path and the project path. It’s a lot of repetition and error prone if you’re scoping using subpaths and/or need to use the paths for custom targets.

Using variables allow for cleaner values and more portability.

Suggested Implementation

Suppose there are 2 variables available: NX_ROOT and NX_PROJECT. A project.json file would only need to use those variables to reference the monrepo root and the project path:

{
  "root": "$NX_PROJECT",
  "sourceRoot": "$NX_PROJECT/src",
  "projectType": "application",
  "targets": {
    "package": {
      "executor": "@nrwl/workspace:run-commands",
      "outputs": ["{options.outputPath}"],
      "options": {
        "outputPath": "$NX_ROOT/dist/serverless/payment.package",
        "commands": [
          "rm -rf $NX_ROOT/dist/serverless/payment.package",
          "pnpx nx run serverless-payment-app:build --with-deps",
          "cd $NX_PROJECT && $NX_ROOT/node_modules/.bin/serverless package --config serverless.ts --package $NX_ROOT/dist/serverless/payment.package"
        ],
        "parallel": false
    }
  }
}

Those variables would automatically be expanded by NX. Also, those variables would be made available as environment variables to any command/shell executed by a target.

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:44
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

13reactions
chrisuicommented, Dec 8, 2021

Seems like an ideal place to ask where the "{}" syntax in targets[].outputs[] comes from and why the syntax for variable expansion would not be consistent?

Eg.

{
  "root": "{project.dir}",
  "sourceRoot": "{project.dir}/src",
  "projectType": "library",
  "targets": {
    "lint": {
      "executor": "@nrwl/linter:eslint",
      "outputs": ["{options.outputFile}"],
      "options": {
        "lintFilePatterns": ["{project.dir}/**/*.{ts,tsx,js,jsx}"]
      }
    }
  },
  "tags": [],
  "implicitDependencies": []
}

9reactions
popovicsandrascommented, Aug 31, 2021

Hello.

We have some similar needs, especially because we have some apps in our monorepo, which need to be mirrored into a public repo (community distributions), with their own angular.json. At the moment, we needed to duplicate the content of angular.json files related to those particular apps, but with the current approach (version 2), we would still need to have duplications since the paths are hardcoded into the project.json.

I really like the suggestion from @diegovilar, however I reckon, using a different notation might be better, to avoid those template strings ($NX_ROOT , $NX_PROJECT ) to be mistaken with shell environment variables. What about following something similar which I saw in the jest config for example?

  • <nx_root>/…
  • <nx_project>/…
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to Set Up Environment Variables using JSON files with ...
In this article, I'll take the result of those tutorials as a starting point to set up environment variables using JSON files.
Read more >
About the Project.Json File - UiPath Documentation Portal
Project.json is an automatically generated file which is created for each .xaml file marked as Main in the project folder. The file holds...
Read more >
Injecting Environment Variables into JSON Files
Methods for injecting dynamic variables, like environment variables, into normally static files such as JSON and YAML.
Read more >
How To Use .env Variables In package.json - GenUI
By using dotenv and cross-var together, we're able to read in whichever .env files we want, or consume existing environment variables (from cli,...
Read more >
Variables reference - Visual Studio Code
A file located at /home/your-username/your-project/folder/file.ext opened in ... Tip: Use IntelliSense inside string values for tasks.json and launch.json ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found