-
Notifications
You must be signed in to change notification settings - Fork 3k
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
feat(pip/_internal): Skip current directory when performing freeze #7810
Conversation
1c7ada5
to
eb876cf
Compare
My changes are failing some tests, but I am unable to find any relation between the failed tests and my changes. |
This probably has the unintended effect of not showing installed packages if the current working directory is the The logic needs to be changed to don’t show a package if it’s in the current working directory, unless the current working directory is one of the entries of |
Thanks for the Pointer. I am just looking into it. |
This test doesn't fail even when I undo all the changes, but it should. As |
On Wed, Mar 4, 2020 at 9:35 AM Tzu-ping Chung ***@***.***> wrote:
> I *think* (without actually running the code) the problem is you’re
running freeze in the wrong directory. The test code roughly translates
to the following commands:
> cd /path/to/pip/tests
> pushd /path/to/cwd
> _fake_install foo-package
> popd
> python -m pip freeze
> The last command run is run in the test directory, not where the
.egg-info is.
Thanks. Well the piece of code, uses `os.getcwd()` to find the current
directory within the tests ie where the tests run. So, shouldn't it be
already present in the same directory where the tests run?
(I couldn't find this comment on GitHub, therefore replying here.)
Regards
Prashant Sharma
|
On a bit of experimentation. I wrote a simple test as
But the test fails. Shouldn't it actually be passed? Its failure results in the report
@pradyunsg @uranusjr, can you have a look at it? Is this the intended behaviour or is there something that I am missing? What I can understand is that there is a difference in invoking pip. But with |
ping @pradyunsg ^^ 😅 |
I don't think modifying pkg_resources is the way to go here... We instead want to more directly specify where diff --git a/src/pip/_internal/commands/freeze.py b/src/pip/_internal/commands/freeze.py
index 4758e303..55d475e4 100644
--- a/src/pip/_internal/commands/freeze.py
+++ b/src/pip/_internal/commands/freeze.py
@@ -83,12 +83,17 @@ class FreezeCommand(Command):
cmdoptions.check_list_path_option(options)
+ paths = options.path
+ # Filter sys.path, to avoid listing distributions from current directory
+ if paths is None:
+ paths = [item for item in sys.path if item]
+
freeze_kwargs = dict(
requirement=options.requirements,
find_links=options.find_links,
local_only=options.local,
user_only=options.user,
- paths=options.path,
+ paths=paths,
skip_regex=options.skip_requirements_regex,
isolated=options.isolated_mode,
wheel_cache=wheel_cache, |
Hmm... Looks like I suggest trying the following, and seeing if/how it fails. def test_pip_script(script):
r1 = script.pip("--version")
r2 = script.pip("--version", use_module=False)
assert r1.stdout == r2.stdout |
I performed it this way because the behaviour is not only for |
@gutsytechster For a whole host of reasons, we avoid modifying vendored packages. Think of them as dependencies; we don't want patches on them except those needed to make them work. |
I have updated the PR based on @pradyunsg suggestions and some of my findings. I found that an absolute path of the CWD is also added to What I thought is if current directory is to be added, then it could be using |
ef95a74
to
69bf2d3
Compare
@uranusjr^^ ping 😅 |
# Filter sys.path, to avoid listing distributions from | ||
# current directory | ||
if paths is None: | ||
paths = [item for item in sys.path if item and item != os.getcwd()] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this actually work? sys.path[0]
is ''
(empty string) for me, while os.getcwd()
returns an absolute path. This would probably be
paths = [item for item in sys.path if item and item not in (os.curdir, '')]
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A ''
entry would not be considered as, through the first part of if
statement i.e. if ''
would be False. Since sys.path
contains an absolute path of cwd, apart from ''
, I have used that specific if
statement.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry I missed the if item
part. I do not think sys.path
contains an absolute version of cwd by default though (it does not for me).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It does not actually. However, when you'll print sys.path
within the freeze
operation or in get_installed_distribution()
method, it somehow does.
On the contrary, when I print sys.path
within the WorkingSet
defined in the pkg_resource
it does not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That’s weird. We’ll probably need to figure out why it’s the case before we can come up with a proper fix.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi there, how about
paths = [item for item in sys.path if os.path.abspath(item) != os.path.abspath(os.getcwd())]
since neither of sys.path nor os.getcwd() is warrantied to always give absolute paths.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@McSinyx, the problem is that somehow sys.path
contains absolute path of CWD apart from ''
, which is added by default when you use python -m
. We have to show the package during freeze if the full path is present in sys.path
but not when just ''
is present.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, though the NEWS fragment could use a bit of rephrasing. :)
news/2926.bugfix
Outdated
@@ -0,0 +1 @@ | |||
Skip local directory by default for 'pip freeze' or when invoked as 'python -m pip freeze' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Skip local directory by default for 'pip freeze' or when invoked as 'python -m pip freeze' | |
Avoid listing packages from current directory, when invoked as 'python -m pip freeze' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@pradyunsg, I am unsure about this comment https://github.com/pypa/pip/pull/7810#discussion_r399684766https://github.com/pypa/pip/pull/7810#discussion_r399684766
Also currently the test passes irrespective of changes due to #7864, so it just provides a pseudo confidence.
This skips any *.egg_info present in the current directory when performing pip freeze. This fixes pypa#2926
69bf2d3
to
85c1568
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't merge as is. See #7955 (comment) and following comments.
Closing this since the related issue has been resolved via #7955. :) |
This skips any *.egg_info present in the current directory when
performing pip freeze.
This fixes #2926