Skip to content

Commit 324436c

Browse files
yangdanny97meta-codesync[bot]
authored andcommitted
use absolute path in github output format (#2411)
Summary: Pull Request resolved: #2411 followup for #2392 Reviewed By: grievejia Differential Revision: D93287045 fbshipit-source-id: d89e837b18a6730af7a01ba678fe9af1451818ba
1 parent 7ea1850 commit 324436c

File tree

1 file changed

+23
-46
lines changed

1 file changed

+23
-46
lines changed

pyrefly/lib/commands/check.rs

Lines changed: 23 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ impl OutputFormat {
381381
Self::MinText => Self::write_error_text_to_file(path, relative_to, errors, false),
382382
Self::FullText => Self::write_error_text_to_file(path, relative_to, errors, true),
383383
Self::Json => Self::write_error_json_to_file(path, relative_to, errors),
384-
Self::Github => Self::write_error_github_to_file(path, relative_to, errors),
384+
Self::Github => Self::write_error_github_to_file(path, errors),
385385
Self::OmitErrors => Ok(()),
386386
}
387387
}
@@ -391,46 +391,34 @@ impl OutputFormat {
391391
Self::MinText => Self::write_error_text_to_console(relative_to, errors, false),
392392
Self::FullText => Self::write_error_text_to_console(relative_to, errors, true),
393393
Self::Json => Self::write_error_json_to_console(relative_to, errors),
394-
Self::Github => Self::write_error_github_to_console(relative_to, errors),
394+
Self::Github => Self::write_error_github_to_console(errors),
395395
Self::OmitErrors => Ok(()),
396396
}
397397
}
398398

399-
fn write_error_github(
400-
writer: &mut impl Write,
401-
relative_to: &Path,
402-
errors: &[Error],
403-
) -> anyhow::Result<()> {
399+
fn write_error_github(writer: &mut impl Write, errors: &[Error]) -> anyhow::Result<()> {
404400
for error in errors {
405-
if let Some(command) = github_actions_command(error, relative_to) {
401+
if let Some(command) = github_actions_command(error) {
406402
writeln!(writer, "{command}")?;
407403
}
408404
}
409405
Ok(())
410406
}
411407

412-
fn buffered_write_error_github(
413-
writer: impl Write,
414-
relative_to: &Path,
415-
errors: &[Error],
416-
) -> anyhow::Result<()> {
408+
fn buffered_write_error_github(writer: impl Write, errors: &[Error]) -> anyhow::Result<()> {
417409
let mut writer = BufWriter::new(writer);
418-
Self::write_error_github(&mut writer, relative_to, errors)?;
410+
Self::write_error_github(&mut writer, errors)?;
419411
writer.flush()?;
420412
Ok(())
421413
}
422414

423-
fn write_error_github_to_file(
424-
path: &Path,
425-
relative_to: &Path,
426-
errors: &[Error],
427-
) -> anyhow::Result<()> {
415+
fn write_error_github_to_file(path: &Path, errors: &[Error]) -> anyhow::Result<()> {
428416
let file = File::create(path)?;
429-
Self::buffered_write_error_github(file, relative_to, errors)
417+
Self::buffered_write_error_github(file, errors)
430418
}
431419

432-
fn write_error_github_to_console(relative_to: &Path, errors: &[Error]) -> anyhow::Result<()> {
433-
Self::buffered_write_error_github(stdout(), relative_to, errors)
420+
fn write_error_github_to_console(errors: &[Error]) -> anyhow::Result<()> {
421+
Self::buffered_write_error_github(stdout(), errors)
434422
}
435423
}
436424

@@ -445,10 +433,10 @@ fn severity_to_github_command(severity: Severity) -> Option<&'static str> {
445433
}
446434
}
447435

448-
fn github_actions_command(error: &Error, relative_to: &Path) -> Option<String> {
436+
fn github_actions_command(error: &Error) -> Option<String> {
449437
let command = severity_to_github_command(error.severity())?;
450438
let range = error.display_range();
451-
let file = github_actions_path(error.path().as_path(), relative_to);
439+
let file = github_actions_path(error.path().as_path());
452440
let params = format!(
453441
"file={},line={},col={},endLine={},endColumn={},title={}",
454442
escape_workflow_property(&file),
@@ -465,18 +453,8 @@ fn github_actions_command(error: &Error, relative_to: &Path) -> Option<String> {
465453
const WORKFLOW_DATA_ENCODE_SET: &AsciiSet = &CONTROLS.add(b'%');
466454
const WORKFLOW_PROPERTY_ENCODE_SET: &AsciiSet = &WORKFLOW_DATA_ENCODE_SET.add(b':').add(b',');
467455

468-
fn github_actions_path(path: &Path, relative_to: &Path) -> String {
469-
let relative = if relative_to.as_os_str().is_empty() {
470-
path
471-
} else {
472-
path.strip_prefix(relative_to).unwrap_or(path)
473-
};
474-
let candidate = if relative.as_os_str().is_empty() {
475-
path
476-
} else {
477-
relative
478-
};
479-
let mut path_str = candidate.to_string_lossy().into_owned();
456+
fn github_actions_path(path: &Path) -> String {
457+
let mut path_str = path.to_string_lossy().into_owned();
480458
if std::path::MAIN_SEPARATOR != '/' {
481459
path_str = path_str.replace(std::path::MAIN_SEPARATOR, "/");
482460
}
@@ -1023,7 +1001,6 @@ impl CheckArgs {
10231001

10241002
#[cfg(test)]
10251003
mod tests {
1026-
use std::path::Path;
10271004
use std::path::PathBuf;
10281005
use std::sync::Arc;
10291006

@@ -1052,13 +1029,13 @@ mod tests {
10521029
}
10531030

10541031
#[test]
1055-
fn github_actions_command_includes_relative_path_and_metadata() {
1056-
let cmd = github_actions_command(&sample_error(vec1!["bad".into()]), Path::new("/repo"))
1032+
fn github_actions_command_includes_full_path_and_metadata() {
1033+
let cmd = github_actions_command(&sample_error(vec1!["bad".into()]))
10571034
.expect("should emit command");
10581035
assert!(cmd.starts_with("::error "), "{cmd}");
10591036
assert!(
1060-
cmd.contains("file=foo.py"),
1061-
"relative path expected, got {cmd}"
1037+
cmd.contains("file=/repo/foo.py"),
1038+
"full path expected, got {cmd}"
10621039
);
10631040
assert!(
10641041
cmd.contains("title=Pyrefly bad-assignment"),
@@ -1073,18 +1050,18 @@ mod tests {
10731050
let notice = sample_error(vec1!["bad".into()]).with_severity(Severity::Info);
10741051
let ignored = sample_error(vec1!["bad".into()]).with_severity(Severity::Ignore);
10751052
assert!(
1076-
github_actions_command(&warning, Path::new(""))
1053+
github_actions_command(&warning)
10771054
.unwrap()
10781055
.starts_with("::warning "),
10791056
"warning severity not mapped"
10801057
);
10811058
assert!(
1082-
github_actions_command(&notice, Path::new(""))
1059+
github_actions_command(&notice)
10831060
.unwrap()
10841061
.starts_with("::notice "),
10851062
"info severity not mapped"
10861063
);
1087-
assert!(github_actions_command(&ignored, Path::new("")).is_none());
1064+
assert!(github_actions_command(&ignored).is_none());
10881065
}
10891066

10901067
#[test]
@@ -1100,9 +1077,9 @@ mod tests {
11001077
fn github_output_format_writes_commands() {
11011078
let errors = vec![sample_error(vec1!["bad".into()])];
11021079
let mut buf = Vec::new();
1103-
OutputFormat::write_error_github(&mut buf, Path::new("/repo"), &errors).unwrap();
1080+
OutputFormat::write_error_github(&mut buf, &errors).unwrap();
11041081
let output = String::from_utf8(buf).unwrap();
1105-
assert!(output.contains("::error file=foo.py"));
1082+
assert!(output.contains("::error file=/repo/foo.py"));
11061083
assert!(output.ends_with("::bad\n"));
11071084
}
11081085
}

0 commit comments

Comments
 (0)