Skip to content

Commit

Permalink
fix: Handful of fixes. (#1176)
Browse files Browse the repository at this point in the history
* Add files.

* More stuff.

* More stuff.

* Update docs.

* Fix globs.

* Add files.

* Dont apply to root.

* Fix root handling.
  • Loading branch information
milesj authored Nov 14, 2023
1 parent 1828e0b commit 2047645
Show file tree
Hide file tree
Showing 37 changed files with 344 additions and 25 deletions.
2 changes: 1 addition & 1 deletion crates/node/platform/src/actions/sync_project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub async fn sync_project(
// Update dependencies within this project's `package.json`.
// Only add if the dependent project has a `package.json`,
// and this `package.json` has not already declared the dep.
if node_config.sync_project_workspace_dependencies {
if node_config.sync_project_workspace_dependencies && !project.is_root_level() {
let format = &node_config.dependency_version_format;

if let Some(dep_package_json) = PackageJson::read(&dep_project.root)? {
Expand Down
2 changes: 1 addition & 1 deletion nextgen/project-builder/src/project_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ impl<'app> ProjectBuilder<'app> {
id = self.id.as_str(),
dep = dep_id.as_str(),
task = task_config.target.as_str(),
"Marking arbitrary project as a peer dependency because of a task dependency"
"Marking arbitrary project as an implicit dependency because of a task dependency"
);

deps.insert(
Expand Down
74 changes: 52 additions & 22 deletions nextgen/project-graph/src/projects_locator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,17 @@ use tracing::warn;

/// Infer a project name from a source path, by using the name of
/// the project folder.
pub fn infer_project_id_and_source(path: &str) -> miette::Result<(Id, WorkspaceRelativePathBuf)> {
pub fn infer_project_id_and_source(
path: &str,
workspace_root: &Path,
) -> miette::Result<(Id, WorkspaceRelativePathBuf)> {
if path.is_empty() {
return Ok((
Id::clean(fs::file_name(workspace_root))?,
WorkspaceRelativePathBuf::from("."),
));
}

let (id, source) = if path.contains('/') {
(path.split('/').last().unwrap().to_owned(), path)
} else {
Expand All @@ -20,41 +30,61 @@ pub fn infer_project_id_and_source(path: &str) -> miette::Result<(Id, WorkspaceR

/// For each pattern in the globs list, glob the file system
/// for potential projects, and infer their name and source.
pub fn locate_projects_with_globs<I, V>(
pub fn locate_projects_with_globs<'glob, I, V>(
workspace_root: &Path,
globs: I,
sources: &mut FxHashMap<Id, WorkspaceRelativePathBuf>,
vcs: Option<&BoxedVcs>,
) -> miette::Result<()>
where
I: IntoIterator<Item = V>,
V: AsRef<str>,
I: IntoIterator<Item = &'glob V>,
V: AsRef<str> + 'glob,
{
let root_source = ".".to_owned();
let globs = globs
.into_iter()
.map(|glob| glob.as_ref().to_owned())
.collect::<Vec<_>>();
let mut locate_globs = vec![];
let mut has_root_level = sources.values().any(|source| source == ".");

// Root-level project has special handling
if globs.contains(&root_source) {
let root_id = fs::file_name(workspace_root);
for glob in globs.into_iter() {
let glob = glob.as_ref();

sources.insert(
Id::clean(if root_id.is_empty() {
"root"
} else {
root_id.as_str()
})?,
WorkspaceRelativePathBuf::from(root_source),
);
if glob == "." {
if has_root_level {
continue;
}

let (id, source) = infer_project_id_and_source("", workspace_root)?;

has_root_level = true;
sources.insert(id, source);
} else {
locate_globs.push(glob);
}
}

// Glob for all other projects
let mut potential_projects = glob::walk(workspace_root, &globs)?;
let mut potential_projects = glob::walk(workspace_root, locate_globs)?;
potential_projects.sort();

for project_root in potential_projects {
for mut project_root in potential_projects {
// Remove trailing moon.yml
if project_root.is_file() {
if project_root.ends_with(consts::CONFIG_PROJECT_FILENAME) {
project_root = project_root.parent().unwrap().to_owned();

// Avoid overwriting an existing root project
if project_root == workspace_root && has_root_level {
continue;
}
} else {
warn!(
source = ?project_root,
"Received a file path for a project root, must be a directory",
);

continue;
}
}

if project_root.is_dir() {
let project_source =
to_virtual_string(project_root.strip_prefix(workspace_root).unwrap())?;
Expand All @@ -77,7 +107,7 @@ where
}
}

let (id, source) = infer_project_id_and_source(&project_source)?;
let (id, source) = infer_project_id_and_source(&project_source, workspace_root)?;

if let Some(existing_source) = sources.get(&id) {
warn!(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
Empty file.
14 changes: 14 additions & 0 deletions nextgen/project-graph/tests/project_graph_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,20 @@ mod project_graph {
);
}

#[tokio::test]
async fn globs_with_config() {
let sandbox = create_sandbox("locate-configs");
let mut container = ProjectGraphContainer::new(sandbox.path());

container.workspace_config.projects =
WorkspaceProjects::Globs(string_vec!["*/moon.yml"]);

let context = container.create_context();
let graph = container.build_graph(context).await;

assert_eq!(get_ids_from_projects(graph.get_all().unwrap()), ["a", "c"]);
}

#[tokio::test]
async fn paths() {
let sandbox = create_sandbox("dependencies");
Expand Down
8 changes: 7 additions & 1 deletion packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@

## Unreleased

#### 🐞 Fixes
#### 🚀 Updates

- Updated `projects` globs to support ending in `moon.yml`.
- Updated `node.dependencyVersionFormat` to fallback to a supported format when the chosen
`node.packageManager` does not support the configured (or default) version format.

#### 🐞 Fixes

- Fixed an issue where dependencies were being injected into the root `package.json`, when a
root-level project was dependending on non-root project tasks.

## 1.16.1

#### 🐞 Fixes
Expand Down
1 change: 1 addition & 0 deletions tests/repros/1174/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.moon/cache
35 changes: 35 additions & 0 deletions tests/repros/1174/.moon/tasks/tag-component.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
fileGroups:
sources:
- '**/*'
projectConfigs:
- '*.json'
- '*.js'

tasks:
buildComponent:
# command: 'node ../../scripts/build-component.js .'
command: 'echo $project'
deps:
- '^:buildComponent'
- '~:buildComponentTypes'
inputs:
- '@globs(sources)'
- '@group(projectConfigs)'
# outputs:
# - 'dist/index.js'
# - 'dist/index.js.map'
# - 'dist/index.mjs'
# - 'dist/index.mjs.map'
platform: 'system'
buildComponentTypes:
command: 'tsc --build'
deps:
- '^:build'
inputs:
- '@globs(sources)'
- '@group(projectConfigs)'
outputs:
# - 'dist/src'
# - 'dist/test'
# - 'dist/tsconfig.tsbuildinfo'
- 'dist'
63 changes: 63 additions & 0 deletions tests/repros/1174/.moon/toolchain.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# Configures Node.js within the toolchain. moon manages its own version of Node.js
# instead of relying on a version found on the host machine. This ensures deterministic
# and reproducible builds across any machine.
node:
# The version to use. Must be a semantic version that includes major, minor, and patch.
# We suggest using the latest active LTS version: https://nodejs.org/en/about/releases
version: '18.17.0'

# The package manager to use when managing dependencies.
# Accepts "npm" (default), "pnpm", or "yarn".
packageManager: 'pnpm'

# The version of the package manager (above) to use.
# yarn:
# version: '3.6.3'

# Add `node.version` as a constraint in the root `package.json` `engines`.
addEnginesConstraint: true

# Dedupe dependencies after the lockfile has changed.
dedupeOnLockfileChange: true

# Version format to use when syncing dependencies within the project's `package.json`.
dependencyVersionFormat: 'workspace-caret'

# Infer and automatically create moon tasks from `package.json` scripts, per project.
# BEWARE: Tasks and scripts are not 1:1 in functionality, so please refer to the documentation.
inferTasksFromScripts: false

# Sync a project's `dependsOn` as dependencies within the project's `package.json`.
syncProjectWorkspaceDependencies: true

# Sync `node.version` to a 3rd-party version manager's config file.
# Accepts "nodenv" (.node-version), "nvm" (.nvmrc), or none.
# syncVersionManagerConfig: 'nvm'

# Configures how moon integrates with TypeScript.
typescript:
# When `syncProjectReferences` is enabled and a dependent project reference
# *does not* have a `tsconfig.json`, automatically create one.
createMissingConfig: true

# Name of `tsconfig.json` file in each project root.
# projectConfigFileName: 'tsconfig.json'

# Name of `tsconfig.json` file in the workspace root.
# rootConfigFileName: 'tsconfig.json'

# Name of the config file in the workspace root that defines shared compiler
# options for all project reference based config files.
# rootOptionsConfigFileName: 'tsconfig.options.json'

# Update a project's `tsconfig.json` to route the `outDir` compiler option
# to moon's `.moon/cache` directory.
routeOutDirToCache: false

# Sync a project's `dependsOn` as project references within the
# project's `tsconfig.json` and the workspace root `tsconfig.json`.
syncProjectReferences: false

# Sync a project's project references as import aliases to the `paths`
# compiler option in each applicable project.
syncProjectReferencesToPaths: false
2 changes: 2 additions & 0 deletions tests/repros/1174/.moon/workspace.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
projects:
- '*'
8 changes: 8 additions & 0 deletions tests/repros/1174/base/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tags: [component]

workspace:
inheritedTasks:
exclude:
- 'build'
rename:
buildComponent: build
4 changes: 4 additions & 0 deletions tests/repros/1174/base/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "base",
"version": "1.0.0"
}
Empty file.
6 changes: 6 additions & 0 deletions tests/repros/1174/base/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist"
},
"include": ["**/*"]
}
8 changes: 8 additions & 0 deletions tests/repros/1174/chat/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tags: [component]

workspace:
inheritedTasks:
exclude:
- 'build'
rename:
buildComponent: build
15 changes: 15 additions & 0 deletions tests/repros/1174/chat/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "chat",
"version": "1.0.0",
"peerDependencies": {
"container": "^1.0.0",
"base": "^1.0.0"
},
"devDependencies": {
"container": "workspace:1.0.0",
"base": "workspace:1.0.0"
},
"dependencies": {
"typescript": "^5.2.2"
}
}
Empty file.
6 changes: 6 additions & 0 deletions tests/repros/1174/chat/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist"
},
"include": ["**/*"]
}
8 changes: 8 additions & 0 deletions tests/repros/1174/container/moon.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
tags: [component]

workspace:
inheritedTasks:
exclude:
- 'build'
rename:
buildComponent: build
4 changes: 4 additions & 0 deletions tests/repros/1174/container/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "container",
"version": "1.0.0"
}
Empty file.
6 changes: 6 additions & 0 deletions tests/repros/1174/container/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"compilerOptions": {
"outDir": "dist"
},
"include": ["**/*"]
}
10 changes: 10 additions & 0 deletions tests/repros/1174/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"workspaces": [
"base",
"chat",
"container"
],
"engines": {
"node": "18.17.0"
}
}
34 changes: 34 additions & 0 deletions tests/repros/1174/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2047645

Please sign in to comment.