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

yarn: Fix state=latest not working with global=true #5829

Merged
merged 11 commits into from
Feb 13, 2023

Conversation

sargunv
Copy link
Contributor

@sargunv sargunv commented Jan 13, 2023

SUMMARY

Fixes #5712.

  • Updates _exec to support running Yarn commands that wouldn't ordinarily be available with yarn global by setting the working directory to the path from yarn global dir instead.
  • Updates list_outdated to use this new functionality when invoking yarn outdated.
  • Updates list_outdated to check for actual errors in stderr output instead of failing on the presence of anything in stderr. This is b/c the Yarn global package.json has no license field, causing a warning to be logged to stderr when invoking yarn outdated in that directory.

Through testing, found a couple more related bugs to fix (see comments below):

  • Replaces the hardcoded DEFAULT_GLOBAL_INSTALLATION_PATH with code to check the output of yarn global dir, as the default path may be different when the use is root, or when the user has set YARN_GLOBAL_FOLDER or the corresponding config flag.
  • Updates list to function correctly for packages without binaries (regular libraries) when global=true by invoking yarn list without global, the same way I did for yarn outdated above.

And finally, added tests for all the above cases. Previously, there were no tests for global=true at all.

ISSUE TYPE
  • Bugfix Pull Request
COMPONENT NAME

yarn

ADDITIONAL INFORMATION

Before:

image

After:

image

@sargunv sargunv changed the title Yarn module: fix state=latest not working with global=true Fix state=latest not working with global=true in yarn module Jan 13, 2023
@ansibullbot
Copy link
Collaborator

@ansibullbot ansibullbot added bug This issue/PR relates to a bug language module module packaging plugins plugin (any type) labels Jan 13, 2023
Comment on lines 215 to 217
if self.globally and unsupported_with_global:
_rc, out, _err = self.module.run_command(self.executable + ['global', 'dir'], check_rc=check_rc)
path = out.strip()
Copy link
Contributor Author

@sargunv sargunv Jan 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does mean a global=true param will override the path param, but that was previously true anyway due to this line:

if self.path and not self.globally:

@felixfontein felixfontein added check-before-release PR will be looked at again shortly before release and merged if possible. backport-5 labels Jan 13, 2023
@russoz russoz changed the title Fix state=latest not working with global=true in yarn module yarn: Fix state=latest not working with global=true Jan 16, 2023
@russoz
Copy link
Collaborator

russoz commented Jan 16, 2023

Hi @sargunv
Thank you for the contribution! Please add a changelog fragment.

Also, if you could please add a testcase in the integration tests, so future modifications in this module take that change into account.

@ansibullbot ansibullbot added integration tests/integration tests tests labels Jan 16, 2023
@sargunv
Copy link
Contributor Author

sargunv commented Jan 16, 2023

There were previously no yarn global tests, so I added test cases to global install, upgrade, and remove a package.

The test for upgrade (state=latest, what this PR fixes) is passing, but the newly added test for remove (state=absent) is failing. I'll have to look into how to get these tests running locally to investigate. Normal usage of global=true with state=absent does work locally.

EDIT: wasn't able to get the tests running locally at all b/c I'm on a distro w/o systemd. I'll just add only the tests relevant to this PR's scope instead of a full set of yarn global tests.

@ansibullbot ansibullbot added the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jan 16, 2023
@ansibullbot ansibullbot removed the needs_revision This PR fails CI tests or a maintainer has requested a review/revision of the PR label Jan 16, 2023
@sargunv
Copy link
Contributor Author

sargunv commented Jan 16, 2023

Got the tests working locally (turns out WSL can enable systemd) and found the issue with the yarn global remove test I had attempted to add.

The yarn module assumes a certain yarn global dir but this assumption is incorrect when running as the root user (as the tests do). When root, the default yarn global dir appears to be /usr/local/share/.config/yarn/global/, not /root/.config/yarn/global/

DEFAULT_GLOBAL_INSTALLATION_PATH = os.path.expanduser('~/.config/yarn/global')

And regardless, hardcoding this path isn't correct as the global dir can be modified by the user with the Yarn config or by environment variable (YARN GLOBAL FOLDER).

@russoz
Copy link
Collaborator

russoz commented Jan 16, 2023

The magic of tests, we find problems we did not imagine we had 😁.

Thanks!

@sargunv
Copy link
Contributor Author

sargunv commented Jan 16, 2023

Fixed that, and while testing that fix I found yet another little bug 🙃

The list() function uses yarn list or yarn global list depending on whether global=true, but yarn global list only lists packages with binaries, not all globally installed packages. So, cases like name=left-pad state=absent global=true do not work; the step thinks left-pad is already absent.

I updated that one to use the same unsupported_with_global param I added for the bug this PR was initially addressing, so list() will always use the regular yarn list. I also removed the bit of code that handled the different output format of yarn global list, and added tests for this case too.

@codecov
Copy link

codecov bot commented Jan 17, 2023

Codecov Report

Merging #5829 (44172dd) into main (58eb495) will decrease coverage by 0.01%.
The diff coverage is 41.66%.

❗ Current head 44172dd differs from pull request most recent head 74f8dfa. Consider uploading reports for the commit 74f8dfa to get more accurate results

@@            Coverage Diff             @@
##             main    #5829      +/-   ##
==========================================
- Coverage   45.54%   45.54%   -0.01%     
==========================================
  Files        1005     1005              
  Lines       97706    97713       +7     
  Branches    17808    17811       +3     
==========================================
+ Hits        44505    44507       +2     
- Misses      51144    51149       +5     
  Partials     2057     2057              
Flag Coverage Δ
integration 71.28% <80.00%> (-0.12%) ⬇️
sanity 21.43% <8.33%> (-0.01%) ⬇️
stub 0.00% <0.00%> (ø)
units 67.86% <0.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
plugins/doc_fragments/ldap.py 100.00% <ø> (ø)
plugins/doc_fragments/rackspace.py 100.00% <ø> (ø)
plugins/modules/consul.py 20.28% <0.00%> (-0.49%) ⬇️
plugins/modules/terraform.py 55.17% <50.00%> (ø)
plugins/doc_fragments/attributes.py 100.00% <100.00%> (ø)
plugins/modules/apache2_module.py 78.75% <100.00%> (+0.26%) ⬆️
plugins/modules/xml.py 80.15% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@ansibullbot ansibullbot added the stale_ci CI is older than 7 days, rerun before merging label Jan 29, 2023
@sargunv
Copy link
Contributor Author

sargunv commented Feb 11, 2023

Hi, is there anything else I need to do to make this PR ready to merge?

Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @sargunv it looks good to me. Only one small issue with the changelog fragment.

@sargunv
Copy link
Contributor Author

sargunv commented Feb 12, 2023

Fixed!

@ansibullbot ansibullbot removed the stale_ci CI is older than 7 days, rerun before merging label Feb 12, 2023
Copy link
Collaborator

@russoz russoz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@felixfontein felixfontein removed the check-before-release PR will be looked at again shortly before release and merged if possible. label Feb 13, 2023
@felixfontein felixfontein merged commit 4c4ef80 into ansible-collections:main Feb 13, 2023
@patchback
Copy link

patchback bot commented Feb 13, 2023

Backport to stable-5: 💔 cherry-picking failed — conflicts found

❌ Failed to cleanly apply 4c4ef80 on top of patchback/backports/stable-5/4c4ef80ca9518792bc4e0551864a7929254a27a0/pr-5829

Backporting merged PR #5829 into main

  1. Ensure you have a local repo clone of your fork. Unless you cloned it
    from the upstream, this would be your origin remote.
  2. Make sure you have an upstream repo added as a remote too. In these
    instructions you'll refer to it by the name upstream. If you don't
    have it, here's how you can add it:
    $ git remote add upstream https://github.com/ansible-collections/community.general.git
  3. Ensure you have the latest copy of upstream and prepare a branch
    that will hold the backported code:
    $ git fetch upstream
    $ git checkout -b patchback/backports/stable-5/4c4ef80ca9518792bc4e0551864a7929254a27a0/pr-5829 upstream/stable-5
  4. Now, cherry-pick PR yarn: Fix state=latest not working with global=true #5829 contents into that branch:
    $ git cherry-pick -x 4c4ef80ca9518792bc4e0551864a7929254a27a0
    If it'll yell at you with something like fatal: Commit 4c4ef80ca9518792bc4e0551864a7929254a27a0 is a merge but no -m option was given., add -m 1 as follows intead:
    $ git cherry-pick -m1 -x 4c4ef80ca9518792bc4e0551864a7929254a27a0
  5. At this point, you'll probably encounter some merge conflicts. You must
    resolve them in to preserve the patch from PR yarn: Fix state=latest not working with global=true #5829 as close to the
    original as possible.
  6. Push this branch to your fork on GitHub:
    $ git push origin patchback/backports/stable-5/4c4ef80ca9518792bc4e0551864a7929254a27a0/pr-5829
  7. Create a PR, ensure that the CI is green. If it's not — update it so that
    the tests and any other checks pass. This is it!
    Now relax and wait for the maintainers to process your pull request
    when they have some cycles to do reviews. Don't worry — they'll tell you if
    any improvements are necessary when the time comes!

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

@patchback
Copy link

patchback bot commented Feb 13, 2023

Backport to stable-6: 💚 backport PR created

✅ Backport PR branch: patchback/backports/stable-6/4c4ef80ca9518792bc4e0551864a7929254a27a0/pr-5829

Backported as #5992

🤖 @patchback
I'm built with octomachinery and
my source is open — https://github.com/sanitizers/patchback-github-app.

patchback bot pushed a commit that referenced this pull request Feb 13, 2023
* Yarn module: fix state=latest not working with global=true

* fix whitespace

* add changelog fragment

* add integration test cases

* add only tests for this PR (install+upgrade)

* fix assuming default global dir

* fix list() not working when global=true and name a package with no binary

* remove ignores

* whitespace

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Felix Fontein <[email protected]>

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Alexei Znamensky <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
Co-authored-by: Alexei Znamensky <[email protected]>
(cherry picked from commit 4c4ef80)
@felixfontein
Copy link
Collaborator

@sargunv thanks for fixing this!
@russoz thanks for reviewing!

felixfontein pushed a commit that referenced this pull request Feb 13, 2023
…king with global=true (#5992)

yarn: Fix state=latest not working with global=true (#5829)

* Yarn module: fix state=latest not working with global=true

* fix whitespace

* add changelog fragment

* add integration test cases

* add only tests for this PR (install+upgrade)

* fix assuming default global dir

* fix list() not working when global=true and name a package with no binary

* remove ignores

* whitespace

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Felix Fontein <[email protected]>

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Alexei Znamensky <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
Co-authored-by: Alexei Znamensky <[email protected]>
(cherry picked from commit 4c4ef80)

Co-authored-by: Sargun Vohra <[email protected]>
@sargunv sargunv deleted the fix-yarn-latest branch February 13, 2023 21:24
jikamens pushed a commit to jikamens/community.general that referenced this pull request Feb 25, 2023
…ons#5829)

* Yarn module: fix state=latest not working with global=true

* fix whitespace

* add changelog fragment

* add integration test cases

* add only tests for this PR (install+upgrade)

* fix assuming default global dir

* fix list() not working when global=true and name a package with no binary

* remove ignores

* whitespace

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Felix Fontein <[email protected]>

* Update changelogs/fragments/5829-fix-yarn-global.yml

Co-authored-by: Alexei Znamensky <[email protected]>

---------

Co-authored-by: Felix Fontein <[email protected]>
Co-authored-by: Alexei Znamensky <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug This issue/PR relates to a bug integration tests/integration language module module packaging plugins plugin (any type) tests tests
Projects
None yet
Development

Successfully merging this pull request may close these issues.

community.general.yarn module fails when provided state:latest and global:true
4 participants