Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

Commit d4f3736

Browse files
authored
Merge pull request #495 from Xanewok/utf8-diagnostics
Preserve UTF-8 file path in the resulting compiler diagnostics
2 parents ad2fc34 + ce13983 commit d4f3736

File tree

6 files changed

+65
-59
lines changed

6 files changed

+65
-59
lines changed

src/actions/post_build.rs

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -236,25 +236,23 @@ fn emit_notifications<O: Output>(
236236
) {
237237
let cwd = ::std::env::current_dir().unwrap();
238238

239-
build_results
240-
.iter()
241-
.for_each(|(path, diagnostics)| {
242-
let params = PublishDiagnosticsParams {
243-
uri: Url::from_file_path(cwd.join(path)).unwrap(),
244-
diagnostics: diagnostics.iter()
245-
.filter_map(|&(ref d, _)| {
246-
if show_warnings || d.severity != Some(DiagnosticSeverity::Warning) {
247-
Some(d.clone())
248-
} else {
249-
None
250-
}
251-
})
252-
.collect(),
253-
};
239+
for (path, diagnostics) in build_results {
240+
let params = PublishDiagnosticsParams {
241+
uri: Url::from_file_path(cwd.join(path)).unwrap(),
242+
diagnostics: diagnostics.iter()
243+
.filter_map(|&(ref d, _)| {
244+
if show_warnings || d.severity != Some(DiagnosticSeverity::Warning) {
245+
Some(d.clone())
246+
} else {
247+
None
248+
}
249+
})
250+
.collect(),
251+
};
254252

255-
out.notify(NotificationMessage::new(
256-
ls_types::NOTIFICATION__PublishDiagnostics,
257-
Some(params),
258-
));
259-
})
253+
out.notify(NotificationMessage::new(
254+
ls_types::NOTIFICATION__PublishDiagnostics,
255+
Some(params),
256+
));
257+
}
260258
}

src/build/rustc.rs

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -74,16 +74,15 @@ pub fn rustc(vfs: &Vfs, args: &[String], envs: &HashMap<String, Option<OsString>
7474

7575
// FIXME(#25) given that we are running the compiler directly, there is no need
7676
// to serialise the error messages - we should pass them in memory.
77-
let stderr_json_msg = convert_message_to_json_strings(Arc::try_unwrap(err_buf)
78-
.unwrap()
79-
.into_inner()
80-
.unwrap());
77+
let err_buf = Arc::try_unwrap(err_buf).unwrap().into_inner().unwrap();
78+
let err_buf = String::from_utf8(err_buf).unwrap();
79+
let stderr_json_msgs: Vec<_> = err_buf.lines().map(String::from).collect();
8180

8281
let analysis = analysis.lock().unwrap().clone();
8382
let analysis = analysis.map(|analysis| vec![analysis]).unwrap_or(vec![]);
8483
match exit_code {
85-
Ok(0) => BuildResult::Success(stderr_json_msg, analysis),
86-
_ => BuildResult::Failure(stderr_json_msg, analysis),
84+
Ok(0) => BuildResult::Success(stderr_json_msgs, analysis),
85+
_ => BuildResult::Failure(stderr_json_msgs, analysis),
8786
}
8887
}
8988

@@ -181,39 +180,6 @@ impl<'a> CompilerCalls<'a> for RlsRustcCalls {
181180
}
182181
}
183182

184-
pub fn convert_message_to_json_strings(input: Vec<u8>) -> Vec<String> {
185-
let mut output = vec![];
186-
187-
// FIXME: this is *so gross* Trying to work around cargo not supporting json messages
188-
let it = input.into_iter();
189-
190-
let mut read_iter = it.skip_while(|&x| x != b'{');
191-
192-
let mut _msg = String::new();
193-
loop {
194-
match read_iter.next() {
195-
Some(b'\n') => {
196-
output.push(_msg);
197-
_msg = String::new();
198-
while let Some(res) = read_iter.next() {
199-
if res == b'{' {
200-
_msg.push('{');
201-
break;
202-
}
203-
}
204-
}
205-
Some(x) => {
206-
_msg.push(x as char);
207-
}
208-
None => {
209-
break;
210-
}
211-
}
212-
}
213-
214-
output
215-
}
216-
217183
/// Tries to read a file from a list of replacements, and if the file is not
218184
/// there, then reads it from disk, by delegating to `RealFileLoader`.
219185
struct ReplacedFileLoader {

src/test/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,3 +670,26 @@ fn test_find_impls() {
670670
// .expect_contains(r#""range":{"start":{"line":19,"character":12},"end":{"line":19,"character":15}}"#)
671671
// ]);
672672
}
673+
674+
#[test]
675+
fn test_handle_utf8_directory() {
676+
let (cache, _tc) = init_env("unicødë");
677+
678+
let root_path = cache.abs_path(Path::new("."));
679+
let root_url = Url::from_directory_path(&root_path).unwrap();
680+
let messages = vec![
681+
initialize(0, root_path.as_os_str().to_str().map(|x| x.to_owned())).to_string()
682+
];
683+
684+
let (mut server, results) = mock_server(messages);
685+
// Initialise and build.
686+
assert_eq!(ls_server::LsService::handle_message(&mut server),
687+
ls_server::ServerStateChange::Continue);
688+
expect_messages(results.clone(), &[ExpectedMessage::new(Some(0)).expect_contains("capabilities"),
689+
ExpectedMessage::new(None).expect_contains("beginBuild"),
690+
ExpectedMessage::new(None).expect_contains("diagnosticsBegin"),
691+
ExpectedMessage::new(None)
692+
.expect_contains(root_url.path())
693+
.expect_contains("struct is never used: `Unused`"),
694+
ExpectedMessage::new(None).expect_contains("diagnosticsEnd")]);
695+
}

test_data/unicødë/Cargo.lock

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test_data/unicødë/Cargo.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[package]
2+
name = "unicødë"
3+
version = "0.1.0"
4+
authors = ["Igor Matuszewski <[email protected]>"]
5+
6+
[dependencies]

test_data/unicødë/src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
struct Unused {}
2+
3+
#[cfg(test)]
4+
mod tests {
5+
#[test]
6+
fn it_works() {
7+
assert_eq!(2 + 2, 4);
8+
}
9+
}

0 commit comments

Comments
 (0)