Skip to content
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

fix(nx-python): relock root when changing deps on grouped projects #195

Merged
merged 4 commits into from
Mar 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 31 additions & 2 deletions packages/nx-python/src/dependency/update-dependency.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { ExecutorContext, ProjectsConfigurations } from '@nx/devkit';
import chalk from 'chalk';
import { getDependents, PyprojectToml } from '../graph/dependency-graph';
import {
getDependents,
PyprojectToml,
PyprojectTomlDependencies,
} from '../graph/dependency-graph';
import {
getProjectTomlPath,
parseToml,
Expand All @@ -27,7 +31,11 @@ export function updateDependencyTree(context: ExecutorContext) {
readFileSync('pyproject.toml', { encoding: 'utf-8' }),
) as PyprojectToml;

if (rootPyprojectToml.tool.poetry.dependencies[pkgName]) {
const allRootDependencyNames = Object.keys(
getAllDependenciesFromPyprojectToml(rootPyprojectToml),
);

if (allRootDependencyNames.includes(pkgName)) {
console.log(
chalk`\nUpdating root {bold pyproject.toml} dependency {bold ${pkgName}}`,
);
Expand Down Expand Up @@ -82,3 +90,24 @@ function getProjectPackageName(context: ExecutorContext, projectName: string) {

return name;
}

/**
* Parses all dependency names from a Pyproject.toml file
* and returns a flattened collection of dependencies
*
* Optionally you may supply a list of groups to ignore
*/
export const getAllDependenciesFromPyprojectToml = (
tomlData: PyprojectToml,
/** optional dependency groups to omit from collection */
omitGroups: string[] = [],
): PyprojectTomlDependencies => {
return {
...(tomlData.tool?.poetry?.dependencies ?? {}),
...Object.fromEntries(
Object.entries(tomlData.tool?.poetry?.group ?? {})
.filter(([name]) => !omitGroups.includes(name))
.flatMap(([_, group]) => Object.entries(group.dependencies ?? {})),
),
};
};
78 changes: 78 additions & 0 deletions packages/nx-python/src/executors/add/executor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1066,4 +1066,82 @@ version = "1.0.0"
});
expect(output.success).toBe(true);
});

it('run add target and should add the dependency to the project using --lock when the root pyproject.toml is present when project is grouped in root', async () => {
fsMock({
'pyproject.toml': dedent`
[tool.poetry]
name = "app"
version = "1.0.0"

[tool.poetry.dependencies]
python = "^3.8"

[tool.poetry.group.foo.dependencies.app]
path = "apps/app"
develop = true
`,
'apps/app/pyproject.toml': dedent`
[tool.poetry]
name = "app"
version = "1.0.0"
[[tool.poetry.packages]]
include = "app"

[tool.poetry.dependencies]
python = "^3.8"
click = "click"
`,
});

const options = {
name: 'numpy',
local: false,
};

const context = {
cwd: '',
root: '.',
isVerbose: false,
projectName: 'app',
workspace: {
npmScope: 'nxlv',
version: 2,
projects: {
app: {
root: 'apps/app',
targets: {},
},
},
},
};

const output = await executor(options, context);
expect(checkPoetryExecutableMock).toHaveBeenCalled();
expect(activateVenvMock).toHaveBeenCalledWith('.');
expect(spawn.sync).toHaveBeenNthCalledWith(
1,
'poetry',
['add', 'numpy', '--lock'],
{
cwd: 'apps/app',
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(
2,
'poetry',
['lock', '--no-update'],
{
shell: false,
stdio: 'inherit',
},
);
expect(spawn.sync).toHaveBeenNthCalledWith(3, 'poetry', ['install'], {
shell: false,
stdio: 'inherit',
});
expect(output.success).toBe(true);
});
});
Loading