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

Rust as 1st class language #2534

Merged
merged 1 commit into from Oct 11, 2022

Conversation

Holzhaus
Copy link
Contributor

This adds support for pinning a specific rust version and bootstrapping rust with rustup/rustup-init.

I tried to follow the desired behavior described here: #1863 (comment)

@Holzhaus Holzhaus force-pushed the rust-as-1st-class-language branch 3 times, most recently from 06b5339 to 83825f1 Compare October 2, 2022 11:50
@Holzhaus
Copy link
Contributor Author

Holzhaus commented Oct 2, 2022

@asottile Unsure how to remove cargo/rust from the CI env to test the bootstrapping code. Maybe these tests can be added in a separate PR?

@asottile
Copy link
Member

asottile commented Oct 3, 2022

@asottile Unsure how to remove cargo/rust from the CI env to test the bootstrapping code. Maybe these tests can be added in a separate PR?

I think you can probably take the same approach that the other 1st-party languages use? where they stub out the "get default" to return None and observe the results

Comment on lines 106 to 111
if sys.platform == 'win32':
if platform.machine() == 'x86_64':
url = 'https://win.rustup.rs/x86_64'
else:
url = 'https://win.rustup.rs/i686'
else:
Copy link
Member

Choose a reason for hiding this comment

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

I think windows can also run on arm64 -- might need to make a mapping of architectures and platforms to select the url out of that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not a windows expert, but IIUC Windows on ARM has x86 emulation built in: https://learn.microsoft.com/en-us/windows/arm/overview#build-windows-apps-that-run-on-arm

Comment on lines 177 to 179
toolchain = os.getenv('RUSTUP_TOOLCHAIN')
if toolchain is not None:
install_rust_with_toolchain(toolchain)
Copy link
Member

Choose a reason for hiding this comment

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

rather than ping-ponging through environment variables as globals this should just check directly -- I believe that'd be language_version != 'system'

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We still need the toolchain type, but you're right that we don't need to get it from the env. See 7c22138.

pre_commit/languages/rust.py Outdated Show resolved Hide resolved
pre_commit/languages/rust.py Outdated Show resolved Hide resolved
pre_commit/languages/rust.py Outdated Show resolved Hide resolved
pre_commit/languages/rust.py Outdated Show resolved Hide resolved
@asottile
Copy link
Member

asottile commented Oct 3, 2022

anyway -- wanted to say I'm pretty excited for this PR :D

@Holzhaus Holzhaus force-pushed the rust-as-1st-class-language branch 2 times, most recently from 22b2f38 to 85e147c Compare October 4, 2022 12:36
cwd=prefix.prefix_dir,
)
with in_env(prefix, version):
toolchain = TOOLCHAIN_FROM_VERSION.get(version, version)
Copy link
Member

Choose a reason for hiding this comment

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

I don't think TOOLCHAIN_FROM_VERSION is all that useful -- this condition here is just if version != 'system': rather than needing to go through this mapping

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I only added the mapping to have a single source of truth for mapping C.DEFAULT to stable. I think I incorporated the changes in 8d545ad

pre_commit/languages/rust.py Outdated Show resolved Hide resolved
pre_commit/languages/rust.py Outdated Show resolved Hide resolved
pre_commit/languages/rust.py Outdated Show resolved Hide resolved
tests/languages/rust_test.py Outdated Show resolved Hide resolved
Comment on lines 92 to 99
toml.dumps({
'package': {
'name': 'hello_world',
'version': '0.1.0',
'edition': '2021',
},
'dependencies': {},
}),
Copy link
Member

Choose a reason for hiding this comment

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

can probably skip toml and just write a string here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I adapted this from the node tests which use json.dumps() for the package.json instead of writing a string directly. And we have a dependency on toml for reading the Cargo.toml file anyway. But I can change it if you want.

@Holzhaus
Copy link
Contributor Author

Holzhaus commented Oct 4, 2022

The windows tests are failing because Windows has problems with long paths:

E           pre_commit.util.CalledProcessError: command: ('C:\\Users\\VssAdministrator\\.cargo\\bin\\cargo.EXE', 'install', '--bins', '--root', 'C:\\Users\\VssAdministrator\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_local_rust_additional_dep0\\0\\.pre-commit\\repo04ryo5oe\\rustenv-system', 'hello-cli', '--version', '0.2.2')
E           return code: 101
E           expected return code: 0
E           stdout: (none)
E           stderr:
E               error: failed to initialize index git repository (in "C:\\Users\\VssAdministrator\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_local_rust_additional_dep0\\0\\.pre-commit\\repo04ryo5oe\\rustenv-system\\registry\\index\\github.com-1ecc6299db9ec823")
E               
E               Caused by:
E                 path too long: 'C:/Users/VssAdministrator/AppData/Local/Temp/pytest-of-unknown/pytest-0/test_local_rust_additional_dep0/0/.pre-commit/repo04ryo5oe/rustenv-system/registry/index/github.com-1ecc6299db9ec823/.git/'; class=Filesystem (30)

Wouldn't have expected that a path of this length is an issue in 2022, but here we are now. Upstream issue seems to be rust-lang/cargo#9770. Ideas?

@asottile
Copy link
Member

asottile commented Oct 4, 2022

The windows tests are failing because Windows has problems with long paths:

E           pre_commit.util.CalledProcessError: command: ('C:\\Users\\VssAdministrator\\.cargo\\bin\\cargo.EXE', 'install', '--bins', '--root', 'C:\\Users\\VssAdministrator\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_local_rust_additional_dep0\\0\\.pre-commit\\repo04ryo5oe\\rustenv-system', 'hello-cli', '--version', '0.2.2')
E           return code: 101
E           expected return code: 0
E           stdout: (none)
E           stderr:
E               error: failed to initialize index git repository (in "C:\\Users\\VssAdministrator\\AppData\\Local\\Temp\\pytest-of-unknown\\pytest-0\\test_local_rust_additional_dep0\\0\\.pre-commit\\repo04ryo5oe\\rustenv-system\\registry\\index\\github.com-1ecc6299db9ec823")
E               
E               Caused by:
E                 path too long: 'C:/Users/VssAdministrator/AppData/Local/Temp/pytest-of-unknown/pytest-0/test_local_rust_additional_dep0/0/.pre-commit/repo04ryo5oe/rustenv-system/registry/index/github.com-1ecc6299db9ec823/.git/'; class=Filesystem (30)

Wouldn't have expected that a path of this length is an issue in 2022, but here we are now. Upstream issue seems to be rust-lang/cargo#9770. Ideas?

might be able to use a shorter temporary directory -- we had TEMP: 'C:\temp' for a while for the windows tests -- not sure if that's still there or not or if we should bring it back

@Holzhaus
Copy link
Contributor Author

Holzhaus commented Oct 4, 2022

I tried to shorten the path passing tox' envtmpdir as pytest --basetemp. This fixes the path length issue. Unfortunately, 3 other tests unrelated to to rust fail now :(

@Holzhaus Holzhaus force-pushed the rust-as-1st-class-language branch 4 times, most recently from 9e48d41 to 852d532 Compare October 5, 2022 12:35
tox.ini Outdated
Comment on lines 1 to 13
[tox]
envlist = py37,py38,pypy3,pre-commit
# Platform specification support is available since version 2.0, see:
# https://tox.wiki/en/latest/example/platform.html#basic-multi-platform-example
minversion = 2.0
envlist = {py37,py38,pypy3}-{unix,windows},pre-commit

[testenv]
deps = -rrequirements-dev.txt
passenv = APPDATA HOME LOCALAPPDATA PROGRAMFILES RUSTUP_HOME
platform =
unix: darwin|cygwin|linux\d*|aix\d*|sunos\d*|freebsd\d*
windows: win32
commands =
coverage erase
coverage run -m pytest {posargs:tests}
coverage report
unix: coverage erase
coverage run -m pytest --basetemp="/tmp/pytest-{envname}" {posargs:tests}
coverage report
windows: coverage erase
coverage run -m pytest --basetemp="C:\tmp\pytest-{envname}" {posargs:tests}
coverage report

[testenv:pre-commit]
skip_install = true
Copy link
Member

Choose a reason for hiding this comment

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

I'd rather not change this -- it's intentionally simple. I meant to just add the temp override in azure-pipelines.yml since that's really the only place the paths are going to be so deep

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, I have zero experience with Azure pipelines and I cannot really spend more time on this. You should have push acess to my branch. I'll remove the commit that changes the tox.ini. It would be nice if you could take of this.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@asottile Thanks for taking care of this!

@Holzhaus
Copy link
Contributor Author

Holzhaus commented Oct 5, 2022

Looks like CI passed except for the failing test_pre_merge_commit_integration on Windows, but that one also fails on current main so it's not related to this PR. Anything else to do?

@Holzhaus Holzhaus requested a review from asottile October 7, 2022 09:54
Copy link
Member

@asottile asottile left a comment

Choose a reason for hiding this comment

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

@asottile asottile merged commit 529b1a6 into pre-commit:main Oct 11, 2022
@Holzhaus Holzhaus deleted the rust-as-1st-class-language branch October 11, 2022 06:30
@Holzhaus
Copy link
Contributor Author

Any ETA when this will land in a stable release?

I have a rust pre-commit hook that I want to use on a non-rust project and don't want to bother contributors with setting up rust themselves, so my plan is to pin a rust toolchain and set minimum_pre_commit_version to 2.21.0 to prevent people from running into issues.

Holzhaus added a commit to Holzhaus/mixxx that referenced this pull request Oct 20, 2022
Mixxx is a C project, and requiring contributors to set up a rust
installation on their systems just to be able to use `qml_formatter` is
a bit tedious.

Fortunately, pre-commit 2.21.0+ features support for bootstrapping Rust
toolchains using rustup, so that no preexisting system install is necessary.
See pre-commit/pre-commit#2534 for details.

We set pre-commit 2.21.0 as the minimum required version, to prevent
users that use an older pre-commit version and don't have rust installed
from running into problems and to inform them that they should update.

If someone uses an pre-commit version < 2.21.0, the following error message
will be shown:

    $ pre-commit run
    An error has occurred: InvalidConfigError:
    ==> File .pre-commit-config.yaml
    ==> At Config()
    ==> At key: minimum_pre_commit_version
    =====> pre-commit version 2.21.0 is required but version 2.20.0 is installed.  Perhaps run `pip install --upgrade pre-commit`.
    Check the log at /home/user/.cache/pre-commit/pre-commit.log
@asottile
Copy link
Member

it's sorta rude to demand releases like that -- it'll happen when it happens

@Holzhaus
Copy link
Contributor Author

Holzhaus commented Oct 20, 2022

it's sorta rude to demand releases like that -- it'll happen when it happens

I'm sorry, that was not my intention. I didn't mean to be pushy. I was just interested because I don't know what the release roadmap for pre-commit is. If there is something blocking the release, I can try to help out.

Holzhaus added a commit to Holzhaus/mixxx that referenced this pull request Jan 19, 2023
Mixxx is a C project, and requiring contributors to set up a rust
installation on their systems just to be able to use `qml_formatter` is
a bit tedious.

Fortunately, pre-commit 2.21.0+ features support for bootstrapping Rust
toolchains using rustup, so that no preexisting system install is necessary.
See pre-commit/pre-commit#2534 for details.

We set pre-commit 2.21.0 as the minimum required version, to prevent
users that use an older pre-commit version and don't have rust installed
from running into problems and to inform them that they should update.

If someone uses an pre-commit version < 2.21.0, the following error message
will be shown:

    $ pre-commit run
    An error has occurred: InvalidConfigError:
    ==> File .pre-commit-config.yaml
    ==> At Config()
    ==> At key: minimum_pre_commit_version
    =====> pre-commit version 2.21.0 is required but version 2.20.0 is installed.  Perhaps run `pip install --upgrade pre-commit`.
    Check the log at /home/user/.cache/pre-commit/pre-commit.log
Holzhaus added a commit to Holzhaus/mixxx that referenced this pull request Jan 19, 2023
Mixxx is a C project, and requiring contributors to set up a rust
installation on their systems just to be able to use `qml_formatter` is
a bit tedious.

Fortunately, pre-commit 2.21.0+ features support for bootstrapping Rust
toolchains using rustup, so that no preexisting system install is necessary.
See pre-commit/pre-commit#2534 for details.

We set pre-commit 2.21.0 as the minimum required version, to prevent
users that use an older pre-commit version and don't have rust installed
from running into problems and to inform them that they should update.

If someone uses an pre-commit version < 2.21.0, the following error message
will be shown:

    $ pre-commit run
    An error has occurred: InvalidConfigError:
    ==> File .pre-commit-config.yaml
    ==> At Config()
    ==> At key: minimum_pre_commit_version
    =====> pre-commit version 2.21.0 is required but version 2.20.0 is installed.  Perhaps run `pip install --upgrade pre-commit`.
    Check the log at /home/user/.cache/pre-commit/pre-commit.log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants