Skip to content

Deleting the only app in a workspace makes CLI unusable #3511

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

Closed
jdforsythe opened this issue Aug 13, 2020 · 6 comments · Fixed by #3528
Closed

Deleting the only app in a workspace makes CLI unusable #3511

jdforsythe opened this issue Aug 13, 2020 · 6 comments · Fixed by #3528
Assignees

Comments

@jdforsythe
Copy link
Contributor

Current Behavior

Deleting all apps in the workspace leaves the CLI unusable. It fails with Project 'testapp' could not be found in workspace due to the app still being listed in the defaultProject property in angular.json

Expected Behavior

Deleting the app should not make the CLI fail.

Steps to Reproduce

  • npx create-nx-workspace@latest
    • name: testtorg
    • application: angular
    • app name: testapp
    • styles: CSS
    • cloud: no
  • cd testorg
  • code .
  • open nx Console tab in VSCode
  • click Generate
  • choose @nrwl/workspace - remove
    • enter projectName testapp-e2e and click "Run" (e2e app is deleted)
    • enter projectName testapp and click "Run" (app is deleted)
  • close the Generate tab
  • click Generate again
  • choose @nrwl/angular - application
  • terminal begins showing Project 'testapp' could not be found in workspace errors on all the dry run commands and any command you try to run (e.g. entering a name and clicking "Run" results in the same error
  • CLI commands give the same error, e.g. nx g @nrwl/angular:app -> Project 'testapp' could not be found in workspace.

Failure Logs

Environment

@jdforsythe
Copy link
Contributor Author

I think something like this would work, but it involves editing angular.json from the @nrwl/workspace project and that doesn't seem right. Any ideas on a better way?

diff --git a/packages/workspace/src/schematics/remove/lib/update-angular-json.ts b/packages/workspace/src/schematics/remove/lib/update-angular-json.ts
new file mode 100644
index 00000000..32622141
--- /dev/null
+++ b/packages/workspace/src/schematics/remove/lib/update-angular-json.ts
@@ -0,0 +1,17 @@
+import { NxJson, updateJsonInTreeIfExists } from '@nrwl/workspace';
+import { Schema } from '../schema';
+
+/**
+ * Updates the angular.json file to remove the project as defaultProject
+ *
+ * @param schema The options provided to the schematic
+ */
+export function updateAngularJson(schema: Schema) {
+  return updateJsonInTreeIfExists<NxJson>('angular.json', (json) => {
+    if (json.defaultProject && json.defaultProject === schema.projectName) {
+      delete json.defaultProject;
+    }
+
+    return json;
+  });
+}
diff --git a/packages/workspace/src/schematics/remove/remove.ts b/packages/workspace/src/schematics/remove/remove.ts
index b409917c..9e241323 100644
--- a/packages/workspace/src/schematics/remove/remove.ts
+++ b/packages/workspace/src/schematics/remove/remove.ts
@@ -5,6 +5,7 @@ import { checkDependencies } from './lib/check-dependencies';
 import { checkTargets } from './lib/check-targets';
 import { removeProject } from './lib/remove-project';
 import { updateNxJson } from './lib/update-nx-json';
+import { updateAngularJson } from './lib/update-angular-json';
 import { updateTsconfig } from './lib/update-tsconfig';
 import { updateWorkspace } from './lib/update-workspace';
 import { Schema } from './schema';
@@ -16,6 +17,7 @@ export default function (schema: Schema): Rule {
     checkTargets(schema),
     removeProject(schema),
     updateNxJson(schema),
+    updateAngularJson(schema),
     updateTsconfig(schema),
     updateWorkspace(schema),
     formatFiles(schema),
diff --git a/packages/workspace/src/utils/ast-utils.ts b/packages/workspace/src/utils/ast-utils.ts
index 99874dcb..1db34cfc 100644
--- a/packages/workspace/src/utils/ast-utils.ts
+++ b/packages/workspace/src/utils/ast-utils.ts
@@ -493,6 +493,29 @@ export function updateJsonInTree<T = any, O = T>(
   };
 }
 
+/**
+ * This method is specifically for updating JSON in a Tree, but only
+ * if the file already exists
+ * @param path Path of JSON file in the Tree
+ * @param callback Manipulation of the JSON data
+ * @returns A rule which updates a JSON file file in a Tree
+ */
+export function updateJsonInTreeIfExists<T = any, O = T>(
+  path: string,
+  callback: (json: T, context: SchematicContext) => O
+): Rule {
+  return (host: Tree, context: SchematicContext): Tree => {
+    if (!host.exists(path)) {
+      return host;
+    }
+    host.overwrite(
+      path,
+      serializeJson(callback(readJsonInTree(host, path), context))
+    );
+    return host;
+  };
+}
+
 export function updateWorkspaceInTree<T = any, O = T>(
   callback: (json: T, context: SchematicContext, host: Tree) => O
 ): Rule {

@vsavkin vsavkin added the scope: misc Misc issues label Aug 13, 2020
@vsavkin vsavkin assigned vsavkin and catfireparty and unassigned vsavkin Aug 13, 2020
@FrozenPandaz
Copy link
Collaborator

@jdforsythe

What specifically would you expect to happen in the case the defaultProject is removed? Should there be no defaultProject? Should there be a warning?

I think the solution you have is a good start. Please use our updateJsonInTree and getWorkspacePath methods and open a PR!

@jdforsythe
Copy link
Contributor Author

According to what I read of Angular CLI, the default project is just the last one you created. Removing it is fine.

I looked at the updateJsonInTree() but if the file doesn't exist, it will create one. If a user removes an app and has no angular.json file, one shouldn't be created. That's why I created a new function.

@FrozenPandaz
Copy link
Collaborator

Removing it is fine.

👍 I think so as well though I think the user should be warned that they no longer have a defaultProject and they may want to define a new one.

If a user removes an app and has no angular.json file, one shouldn't be created.

I think it is ensured that either a workspace.json or angular.json (getWorkspacePath identifies which one to update) exists in a workspace otherwise, there are errors thrown higher up. Those functions are also used in many other places so if there is an issue, it's more wide-spread than within @nrwl/workspace:remove and can be addressed separately.

@jdforsythe
Copy link
Contributor Author

@FrozenPandaz I pushed PR #3528

Since updateWorkspace() was already being called, I just added the check and warning message there.

I also added tests.

FrozenPandaz pushed a commit that referenced this issue Aug 18, 2020

Verified

This commit was signed with the committer’s verified signature.
novemberborn Mark Wubben
Fixes #3511
Doginal pushed a commit to Doginal/nx that referenced this issue Nov 25, 2020

Verified

This commit was signed with the committer’s verified signature.
novemberborn Mark Wubben
Fixes nrwl#3511
@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 24, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants