Skip to content

Commit

Permalink
new: Add strict project IDs experiment.
Browse files Browse the repository at this point in the history
  • Loading branch information
milesj committed Nov 2, 2024
1 parent af3897e commit 2b44757
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

#### 🚀 Updates

- Added an `experiments.strictProjectIds` setting to `.moon/workspace.yml`. When enabled, will
disallow using original IDs for renamed projects (via the `id` setting) when building the project
graph.
- Updated codegen/template `destination` to be relative from the workspace root if prefixed with
`/`, otherwise the current working directory.

Expand Down
1 change: 1 addition & 0 deletions crates/app/src/components.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub async fn create_project_graph_context(
extend_project: Emitter::<ExtendProjectEvent>::new(),
extend_project_graph: Emitter::<ExtendProjectGraphEvent>::new(),
inherited_tasks: &session.tasks_config,
strict_project_ids: session.workspace_config.experiments.strict_project_ids,
toolchain_config: &session.toolchain_config,
vcs: Some(session.get_vcs_adapter()?),
working_dir: &session.working_dir,
Expand Down
5 changes: 5 additions & 0 deletions crates/config/src/workspace/experiments_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ cacheable!(
#[setting(default = true)]
pub strict_project_aliases: bool,

/// Disallow referencing the original ID of a renamed project when
/// building the project graph.
// #[setting(default = true)]
pub strict_project_ids: bool,

/// Disallow task relationships with different `runInCI` options.
#[setting(default = true)]
pub disallow_run_in_ci_mismatch: bool,
Expand Down
28 changes: 27 additions & 1 deletion crates/project-graph/src/project_graph_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub struct ProjectGraphBuilderContext<'app> {
pub extend_project: Emitter<ExtendProjectEvent>,
pub extend_project_graph: Emitter<ExtendProjectGraphEvent>,
pub inherited_tasks: &'app InheritedTasksManager,
pub strict_project_ids: bool,
pub toolchain_config: &'app ToolchainConfig,
pub vcs: Option<Arc<BoxedVcs>>,
pub working_dir: &'app Path,
Expand Down Expand Up @@ -606,6 +607,7 @@ impl<'app> ProjectGraphBuilder<'app> {
let context = self.context();
let mut configs = FxHashMap::default();
let mut renamed_ids = FxHashMap::default();
let mut dupe_original_ids = FxHashSet::default();

for (id, source) in sources {
debug!(
Expand All @@ -621,14 +623,30 @@ impl<'app> ProjectGraphBuilder<'app> {
// Track ID renames
if let Some(new_id) = &config.id {
if new_id != id {
renamed_ids.insert(id.to_owned(), new_id.to_owned());
if renamed_ids.contains_key(id) {
dupe_original_ids.insert(id.to_owned());
} else {
renamed_ids.insert(id.to_owned(), new_id.to_owned());
}

*id = new_id.to_owned();
}
}

configs.insert(config.id.clone().unwrap_or(id.to_owned()), config);
}

if !dupe_original_ids.is_empty() {
trace!(
original_ids = ?dupe_original_ids.iter().collect::<Vec<_>>(),
"Found multiple projects with the same original ID before being renamed to a custom ID; will ignore these IDs within lookups"
);

for dupe_id in dupe_original_ids {
renamed_ids.remove(&dupe_id);
}
}

debug!("Loaded {} project configs", configs.len());

self.configs.extend(configs);
Expand Down Expand Up @@ -657,6 +675,14 @@ impl<'app> ProjectGraphBuilder<'app> {
}
};

if self
.context
.as_ref()
.is_some_and(|ctx| ctx.strict_project_ids)
{
return id;
}

match self.renamed_ids.get(&id) {
Some(new_id) => new_id.to_owned(),
None => id,
Expand Down
1 change: 1 addition & 0 deletions crates/test-utils/src/project_graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ impl ProjectGraphContainer {
extend_project: Emitter::<ExtendProjectEvent>::new(),
extend_project_graph: Emitter::<ExtendProjectGraphEvent>::new(),
inherited_tasks: &self.inherited_tasks,
strict_project_ids: self.workspace_config.experiments.strict_project_ids,
toolchain_config: &self.toolchain_config,
vcs: self.vcs.clone(),
working_dir: &self.workspace_root,
Expand Down
10 changes: 10 additions & 0 deletions packages/types/src/workspace-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,11 @@ export interface ExperimentsConfig {
* @deprecated
*/
strictProjectAliases?: boolean;
/**
* Disallow referencing the original ID of a renamed project when
* building the project graph.
*/
strictProjectIds: boolean;
/**
* @default true
* @deprecated
Expand Down Expand Up @@ -438,6 +443,11 @@ export interface PartialExperimentsConfig {
* @deprecated
*/
strictProjectAliases?: boolean | null;
/**
* Disallow referencing the original ID of a renamed project when
* building the project graph.
*/
strictProjectIds?: boolean | null;
/**
* @default true
* @deprecated
Expand Down
15 changes: 15 additions & 0 deletions website/docs/config/workspace.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,21 @@ docker:

Enable or disable experiments that alter core functionality.

### `strictProjectIds`<VersionLabel version="1.29.4" />

<HeadingApiLink to="/api/types/interface/ExperimentsConfig#strictProjectIds" />

When building the project graph, disallows referencing the original ID of a project (inferred from
the folder name) when the project has been renamed to a new ID using the [`id`](./project#id)
setting.

Defaults to `false` but will be enabled to `true` in the future.

```yaml title=".moon/workspace.yml" {1,2}
experiments:
strictProjectIds: true
```

## `extensions`<VersionLabel version="1.20.0" />

<HeadingApiLink to="/api/types/interface/WorkspaceConfig#extensions" />
Expand Down
5 changes: 5 additions & 0 deletions website/static/schemas/workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,11 @@
"deprecated": true,
"type": "boolean"
},
"strictProjectIds": {
"title": "strictProjectIds",
"description": "Disallow referencing the original ID of a renamed project when building the project graph.",
"type": "boolean"
},
"taskOutputBoundaries": {
"title": "taskOutputBoundaries",
"default": true,
Expand Down

0 comments on commit 2b44757

Please sign in to comment.