-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
Comments
I think something like this would work, but it involves editing 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 { |
What specifically would you expect to happen in the case the I think the solution you have is a good start. Please use our |
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 |
👍 I think so as well though I think the user should be warned that they no longer have a
I think it is ensured that either a |
@FrozenPandaz I pushed PR #3528 Since I also added tests. |
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. |
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 thedefaultProject
property inangular.json
Expected Behavior
Deleting the app should not make the CLI fail.
Steps to Reproduce
npx create-nx-workspace@latest
testtorg
angular
testapp
CSS
no
cd testorg
code .
@nrwl/workspace - remove
testapp-e2e
and click "Run" (e2e app is deleted)testapp
and click "Run" (app is deleted)@nrwl/angular - application
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 errornx g @nrwl/angular:app
->Project 'testapp' could not be found in workspace.
Failure Logs
Environment
The text was updated successfully, but these errors were encountered: