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

Improve error messaging on bad Pants version #156

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
25 changes: 23 additions & 2 deletions package/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ use tempfile::TempDir;
use termcolor::{Color, WriteColor};

use crate::utils::build::fingerprint;
use crate::utils::exe::{execute, execute_with_input, Platform, CURRENT_PLATFORM};
use crate::utils::exe::{
execute, execute_no_error, execute_with_input, Platform, CURRENT_PLATFORM,
};
use crate::utils::fs::{
copy, create_tempdir, ensure_directory, remove_dir, rename, softlink, touch, write_file,
};
Expand All @@ -38,7 +40,7 @@ fn decode_output(output: Vec<u8>) -> Result<String> {
}

fn assert_stderr_output(command: &mut Command, expected_messages: Vec<&str>) -> Output {
let output = execute(command.stderr(Stdio::piped())).unwrap();
let output = execute_no_error(command.stderr(Stdio::piped()));
Comment on lines 42 to +43
Copy link
Member

Choose a reason for hiding this comment

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

I worry that this does no longer make the same assertion about the sub process' exit code as before for all the unrelated tests using this method now.

Copy link
Author

Choose a reason for hiding this comment

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

Would adding a separate assertion to those call sites make sense? It seems natural for a macro that inspects stderr contents to tolerate nonzero exit codes.

Copy link
Contributor

@huonw huonw Sep 28, 2023

Choose a reason for hiding this comment

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

As of #296, callers of assert_stderr_output can indicate whether the command should either succeed or fail (and both are checked).

let stderr = decode_output(output.stderr.clone()).unwrap();
for expected_message in expected_messages {
assert!(
Expand Down Expand Up @@ -103,6 +105,7 @@ pub(crate) fn run_integration_tests(
test_initialize_new_pants_project(scie_pants_scie);
test_set_pants_version(scie_pants_scie);
test_ignore_empty_pants_version_pants_sha(scie_pants_scie);
test_bad_pants_version(scie_pants_scie);

let clone_root = create_tempdir()?;
test_use_in_repo_with_pants_script(scie_pants_scie, &clone_root);
Expand Down Expand Up @@ -392,6 +395,24 @@ fn test_ignore_empty_pants_version_pants_sha(scie_pants_scie: &Path) {
);
}

fn test_bad_pants_version(scie_pants_scie: &Path) {
integration_test!("Verifying Pants gives good error messages for nonexistent version numbers");
let non_existent_version = "1.2.3.4.5";
assert_stderr_output(
Command::new(scie_pants_scie)
.arg("-V")
.env("PANTS_VERSION", non_existent_version),
vec!["Could not find Pants tag 1.2.3.4.5 in https://github.com/pantsbuild/pants/releases"],
);
let prefix_version = "1";
assert_stderr_output(
Command::new(scie_pants_scie)
.arg("-V")
.env("PANTS_VERSION", prefix_version),
vec!["Could not find Pants tag 1 in https://github.com/pantsbuild/pants/releases"],
);
}

fn test_use_in_repo_with_pants_script(scie_pants_scie: &Path, clone_root: &TempDir) {
integration_test!("Verify scie-pants can be used as `pants` in a repo with the `pants` script");
// This verifies a fix for https://github.com/pantsbuild/scie-pants/issues/28.
Expand Down
16 changes: 12 additions & 4 deletions package/src/utils/exe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,14 +81,22 @@ pub(crate) fn prepare_exe(path: &Path) -> Result<()> {
}

pub(crate) fn execute_with_input(command: &mut Command, stdin_data: &[u8]) -> Result<Output> {
_execute_with_input(command, Some(stdin_data))
_execute_with_input(command, Some(stdin_data), true)
}

pub(crate) fn execute(command: &mut Command) -> Result<Output> {
_execute_with_input(command, None)
_execute_with_input(command, None, true)
}

fn _execute_with_input(command: &mut Command, stdin_data: Option<&[u8]>) -> Result<Output> {
pub(crate) fn execute_no_error(command: &mut Command) -> Output {
_execute_with_input(command, None, false).unwrap()
}

fn _execute_with_input(
command: &mut Command,
stdin_data: Option<&[u8]>,
err_on_command_fail: bool,
) -> Result<Output> {
info!("Executing {command:#?}");
if stdin_data.is_some() {
command.stdin(std::process::Stdio::piped());
Expand All @@ -107,7 +115,7 @@ fn _execute_with_input(command: &mut Command, stdin_data: Option<&[u8]>) -> Resu
let output = child
.wait_with_output()
.with_context(|| format!("Failed to gather exit status of command: {command:?}"))?;
if !output.status.success() {
if err_on_command_fail && !output.status.success() {
let mut message_lines = vec![format!(
"Command {command:?} failed with exit code: {code:?}",
code = output.status.code()
Expand Down
18 changes: 16 additions & 2 deletions tools/src/scie_pants/pants_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from dataclasses import dataclass
from pathlib import Path
from subprocess import CalledProcessError
from typing import Callable, Iterator
from typing import Any, Callable, Dict, Iterator, List
from xml.etree import ElementTree

import tomlkit
Expand Down Expand Up @@ -43,6 +43,10 @@ def pants_find_links_option(self, pants_version_selected: Version) -> str:
return f"--python-repos-{option_name}={operator}['{self.find_links}']"


class TagNotFoundError(Exception):
pass


def determine_find_links(
ptex: Ptex,
pants_version: str,
Expand Down Expand Up @@ -117,12 +121,22 @@ def determine_tag_version(
github_api_url = (
f"https://api.github.com/repos/pantsbuild/pants/git/refs/tags/{urllib.parse.quote(tag)}"
)
github_releases_url = "https://github.com/pantsbuild/pants/releases"
Copy link
Member

Choose a reason for hiding this comment

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

So we're actually about to switch to a GitHub Release-based approach for any version >=2.

If you're able to sit a smidge, this code might look different when we make that switch.

Copy link
Author

Choose a reason for hiding this comment

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

Let me know when that happens and I can revisit.

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for waiting! The switch has happened now.

For version strings that look new (after PANTS_PEX_GITHUB_RELEASE_VERSION), this code path isn't used, as the function returns early. For older ones, definitely still worth considering improvements'; thanks!

For those new ones, Pants is then downloaded from GitHub directly, with some error handling about it not existing (but feel free to fine-tune if you have ideas!), in

pex_url = f"https://github.com/pantsbuild/pants/releases/download/release_{version}/{pex_name}"
if bootstrap_urls_path:
bootstrap_urls = json.loads(Path(bootstrap_urls_path).read_text())
urls_info = bootstrap_urls["ptex"]
pex_url = urls_info.get(pex_name)
if pex_url is None:
raise ValueError(
f"Couldn't find '{pex_name}' in PANTS_BOOTSTRAP_URLS file: '{bootstrap_urls_path}' "
"under the 'ptex' key."
)
if not isinstance(pex_url, str):
raise TypeError(
f"The value for the key '{pex_name}' in PANTS_BOOTSTRAP_URLS file: '{bootstrap_urls_path}' "
f"under the 'ptex' key was expected to be a string. Got a {type(pex_url).__name__}"
)
with tempfile.NamedTemporaryFile(suffix=".pex") as pants_pex:
try:
ptex.fetch_to_fp(pex_url, pants_pex.file)
except subprocess.CalledProcessError as e:
fatal(
f"Wasn't able to fetch the Pants PEX at {pex_url}.\n\n"
"Check to see if the URL is reachable (i.e. GitHub isn't down) and if"
f" {pex_name} asset exists within the release."
" If the asset doesn't exist it may be that this platform isn't yet supported."
" If that's the case, please reach out on Slack: https://www.pantsbuild.org/docs/getting-help#slack"
" or file an issue on GitHub: https://github.com/pantsbuild/pants/issues/new/choose.\n\n"
f"Exception:\n\n{e}"
)

headers = (
{"Authorization": f"Bearer {github_api_bearer_token}"}
if github_api_bearer_token
else {}
)
github_api_tag_url = ptex.fetch_json(github_api_url, **headers)["object"]["url"]
github_api_response: Dict[str, Any]
try:
github_api_response = ptex.fetch_json(github_api_url, **headers)
# If the response is not a dict, it typically means multiple matches were found, which
# means the supplied tag is not an exact match against any tag
if not isinstance(github_api_response, dict):
raise TagNotFoundError(f"Could not find Pants tag {tag} in {github_releases_url}")
except CalledProcessError as e:
raise TagNotFoundError(f"Could not find Pants tag {tag} in {github_releases_url}: {e}")
github_api_tag_url = github_api_response["object"]["url"]
commit_sha = ptex.fetch_json(github_api_tag_url, **headers)["object"]["sha"]

return determine_find_links(
Expand Down