nrwl / nx

Smart Monorepos · Fast CI
https://nx.dev
MIT License
23.82k stars 2.38k forks source link

nx serve always replaces environment.ts file with environment.prod.ts #28534

Closed usarskyy closed 1 month ago

usarskyy commented 1 month ago

Current Behavior

I start one of the apps using nx serve MY_APP_NAME command. My app has a directory called environments with three files in it:

  1. environment.ts
  2. environment.test.ts
  3. environment.prod.ts

In project.json I configured fileReplacements for production and test configurations but not for development.

When I run nx server MY_APP_NAME, I can see that NX executes nx run MY_APP_NAME:serve:development and this target has the following buidTarget in it:

"development": {
          "buildTarget": "MY_APP_NAME:build:development"
        }

I don't understand why environment.ts file gets replaced with environment.prod.ts.

nx reset didn't help.

Expected Behavior

environment.ts file should not be replaced with environment.prod.ts

GitHub Repo

No response

Steps to Reproduce

  1. Simply create an angular app with NX v20, add enironment.ts file for prod configuration and server a development version of an app

Nx Report

Node : 18.19.1 OS : win32-x64 Native Target : x86_64-windows npm : 10.2.4

nx (global) : 20.0.0 nx : 20.0.0 @nx/js : 20.0.0 @nx/jest : 20.0.0 @nx/eslint : 20.0.0 @nx/workspace : 20.0.0 @nx/angular : 20.0.0 @nx/devkit : 20.0.0 @nx/eslint-plugin : 20.0.0 @nx/playwright : 20.0.0 @nx/vite : 20.0.0 @nx/web : 20.0.0 @nx/webpack : 20.0.0 typescript : 5.5.4

Registered Plugins: @nx/playwright/plugin @nx/eslint/plugin

Community plugins: @ngrx/router-store : 18.1.0 @ngrx/store : 18.1.0 @ngrx/store-devtools : 18.1.0

Failure Logs

No response

Package Manager Version

No response

Operating System

Additional Information

No response

usarskyy commented 1 month ago

Mysterious issue is solved: my IDE directly imported environment.prod.ts file, not environment.ts as I expected. Unfortunately I haven't noticed it immediately and wasted lots of time on this problem.

Good news: by experimenting with my project setup I found another bug. So, I tried to add fileReplacements to development configuration and replace a file with itself (I know, sounds stupid but I was desperate :) ) Image Suddenly I got an error saying The E:\ROOT_PROJECT_PATH\src\environments\environment.ts path in file replacements does not exist. This is weird because I added fileReplacements into angular's application project.json file located in directory E:\ROOT_PROJECT_PATH\apps\APP_NAME\, root project.json file wasn't modified. Then I found out that path in fileReplacements is relative to the root directory, not to the app's directory. I could confirm it by launching my app using --configuration=production parameter that failed immediately with the similar error message.

I expect that if I configure file replacement in E:\ROOT_PROJECT_PATH\apps\APP_NAME\project.json, all my paths are relative to this project.json, not to the root project.json.

jaysoo commented 1 month ago

Hi @usarskyy, glad to know you are unblocked on your original issue. As for the paths in project.json, they are always assumed to be relative to the workspace root, not project root, so this is working as intended.

Are you running into any other issue?

usarskyy commented 1 month ago

@jaysoo As for me this behavior looks a bit weird but I am happy it is not a bug :) It would be great if someone could mention this piece of info in all comments for fileReplacements.

github-actions[bot] commented 5 days ago

This issue has been closed for more than 30 days. If this issue is still occuring, please open a new issue with more recent context.