Skip to content

Commit

Permalink
fix(nx-python): replace poetry update with poetry lock and poetry ins…
Browse files Browse the repository at this point in the history
…tall when adding a new package (#243)

currently, when there are more than 2 levels of local projects dependencies,
when a dependency is added to the 2 level or deeper the nx python plugin uses
poetry update <lib> to update to keep all the poetry.lock in sync.

however, the poetry update only works well for the first level, for all the others
the poetry lock is not updated correctly.

the combination of poetry lock and only running poetry install when the root pyproject.toml
is not present, updates the poetry.lock for all the levels of dependencies.
  • Loading branch information
lucasvieirasilva authored Aug 9, 2024
1 parent 4426eb4 commit b311fcb
Show file tree
Hide file tree
Showing 7 changed files with 171 additions and 84 deletions.
3 changes: 1 addition & 2 deletions packages/nx-python/src/dependency/update-dependency.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export function updateDependents(
console.log(chalk`\nUpdating project {bold ${dep}}`);
const depConfig = workspace.projects[dep];

const pkgName = getProjectPackageName(context, projectName);
updateProject(pkgName, depConfig.root, updateLockOnly);
updateProject(depConfig.root, updateLockOnly);

updateDependents(
context,
Expand Down
99 changes: 77 additions & 22 deletions packages/nx-python/src/executors/add/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(4);
expect(spawn.sync).toHaveBeenCalledTimes(7);
expect(spawn.sync).toHaveBeenNthCalledWith(1, 'poetry', ['add', 'numpy'], {
cwd: 'libs/shared1',
shell: false,
Expand All @@ -431,33 +431,48 @@ version = "1.0.0"
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['update', 'shared1'],
['lock', '--no-update'],
{
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['install'], {
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
3,
4,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(5, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
4,
6,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(7, 'poetry', ['install'], {
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -554,7 +569,7 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(4);
expect(spawn.sync).toHaveBeenCalledTimes(7);
expect(spawn.sync).toHaveBeenNthCalledWith(1, 'poetry', ['add', 'numpy'], {
cwd: 'libs/shared1',
shell: false,
Expand All @@ -563,33 +578,48 @@ version = "1.0.0"
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['update', 'shared1'],
['lock', '--no-update'],
{
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['install'], {
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
3,
4,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(5, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
4,
6,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(7, 'poetry', ['install'], {
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -644,17 +674,22 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(1);
expect(spawn.sync).toHaveBeenCalledTimes(2);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -710,17 +745,22 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(1);
expect(spawn.sync).toHaveBeenCalledTimes(2);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -780,17 +820,22 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(1);
expect(spawn.sync).toHaveBeenCalledTimes(2);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -851,17 +896,22 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(1);
expect(spawn.sync).toHaveBeenCalledTimes(2);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -917,17 +967,22 @@ version = "1.0.0"
expect(output.success).toBe(true);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(1);
expect(spawn.sync).toHaveBeenCalledTimes(2);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['update', 'dgx-devops-lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(2, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});

const {
tool: {
Expand Down
4 changes: 2 additions & 2 deletions packages/nx-python/src/executors/add/executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ function updateLocalProject(
dependencyConfig.root,
);

const dependencyPkgName = addLocalProjectToPoetryProject(
addLocalProjectToPoetryProject(
projectConfig,
dependencyConfig,
dependencyPath,
group,
extras,
);
updateProject(dependencyPkgName, projectConfig.root, updateLockOnly);
updateProject(projectConfig.root, updateLockOnly);
}
45 changes: 35 additions & 10 deletions packages/nx-python/src/executors/remove/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(3);
expect(spawn.sync).toHaveBeenCalledTimes(5);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
Expand All @@ -176,23 +176,33 @@ version = "1.0.0"
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
3,
4,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(5, 'poetry', ['install'], {
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down Expand Up @@ -285,7 +295,7 @@ version = "1.0.0"
const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenCalledTimes(4);
expect(spawn.sync).toHaveBeenCalledTimes(7);
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
Expand All @@ -299,33 +309,48 @@ version = "1.0.0"
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['update', 'shared1'],
['lock', '--no-update'],
{
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['install'], {
cwd: 'libs/lib1',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
3,
4,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(5, 'poetry', ['install'], {
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
});
expect(spawn.sync).toHaveBeenNthCalledWith(
4,
6,
'poetry',
['update', 'lib1'],
['lock', '--no-update'],
{
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(7, 'poetry', ['install'], {
cwd: 'apps/app1',
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});

Expand Down
Loading

0 comments on commit b311fcb

Please sign in to comment.