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

Does not modify export command #28

Open
jcassee opened this issue Nov 14, 2024 · 8 comments
Open

Does not modify export command #28

jcassee opened this issue Nov 14, 2024 · 8 comments
Labels
enhancement New feature or request

Comments

@jcassee
Copy link

jcassee commented Nov 14, 2024

When running poetry export, this is the output:

The lock file does not exist. Locking.
Running command from monorepo root directory
Updating dependencies
Resolving dependencies... (7.9s)

[Errno 2] No such file or directory: 'project_root/subproject/poetry.lock'

Does it make sense to also modify the export command?

@ag14774
Copy link
Owner

ag14774 commented Nov 14, 2024

Currently it doesn't support the export command because I didn't personally have a use case for it but it would definitely make sense.

What behavior would you expect from poetry export? Export the root lockfile (so all dependencies of the repo) or select just the ones relevant for the current subproject in the repo?

@ag14774 ag14774 added the enhancement New feature or request label Nov 14, 2024
@jcassee
Copy link
Author

jcassee commented Nov 14, 2024

Good question!

My use-case is the combination with nx.dev, and the nx-python plugin. It generates per-subproject lockfiles, even when using a shared virtual environment.

Selecting only the relevant dependencies would be purest, of course, but I'm already happy with just exporting the whole root lockfile.

@ag14774
Copy link
Owner

ag14774 commented Nov 14, 2024

In that case, I think poetry export at the root of the repo should still work with no changes for exporting the whole root lockfile. I can try to get the exporting of relevant dependencies only in the near future

@jcassee
Copy link
Author

jcassee commented Nov 15, 2024

Indeed, that works, but the nx-python plugin runs it with in the subproject root and expects a lockfile there. (See the output in my first message.)

I do understand this is a pretty particular case. If you have pointers, I might also take a stab at it myself. I did look at the code, but could not understand it with just a casual glance.

@ag14774
Copy link
Owner

ag14774 commented Nov 15, 2024

Of course, feel free to give it a shot! Contributions are always welcome!

I think there are a few things to take into account:

  1. The export command comes from a plugin. The class is poetry_plugin_export.command.ExportCommand. We should try/except importing it to detect whether it is installed or not and modify it only if it is

  2. There are two ways to implement this:
    a) Simply replace the command's poetry instance by calling poetry.set_poetry() and giving it a poetry referring to the root of the repo (see how I do something similar with the lock command). This will allow the poetry export command to run successfully and it will work as if the command was executed at the root dir. I can see that the export command runs exporter = Exporter(self.poetry, self.io) so as long as we set self.poetry to the root poetry before the command executes, this should work.
    b) Generate a temporary lockfile from the root lockfile with the subset of dependencies relevant for the subproject. This would require a bit of understanding of the lockfile format but I am sure poetry has classes for parsing the lockfile. I imagine one could start with the dependencies listed in pyproject.toml of the subproject. Then find those in the root lockfile. In the lockfile, I think one can find the dependencies of those dependencies etc etc. So one could build the graph of dependencies of the subproject and generate the correct lockfile. (maybe you could also probably get inspiration from nx-python as they probably do something similar when creating per-project lockfiles)

@jcassee
Copy link
Author

jcassee commented Nov 16, 2024

I created a very simple PR that implements the first suggestion: #29

The change required was suspiciously simple, the LockCommand just worked.

I'm not sure what kind of tests would be expected.

@ag14774
Copy link
Owner

ag14774 commented Nov 17, 2024

Thank you for the PR! Indeed there is not many changes required.

In order to test the command, you need to install this version of the plugin as a plugin for poetry (e.g as editable install) and then try poetry export in a repo with the plugin enabled.

I tried it and I get an error:

LockModifier can only be used for `poetry lock`, `poetry install`, and `poetry update` commands.

The solution would be to update LockModifier to also accept ExportCommand as a viable choice

I'm not sure what kind of tests would be expected.

I am currently working on improving the testing so I would let this one go without tests for now. I will have a better test suite next week

@ag14774
Copy link
Owner

ag14774 commented Nov 17, 2024

(But that reminds me I should add instructions on how to setup the plugin for development! I created an issue as a reminder)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants