Skip to content

Commit 7a18ba7

Browse files
committed
internal: Enforce utf8 paths
1 parent ba33959 commit 7a18ba7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+373
-308
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ anyhow = "1.0.75"
105105
arrayvec = "0.7.4"
106106
bitflags = "2.4.1"
107107
cargo_metadata = "0.18.1"
108+
camino = "1.1.6"
108109
chalk-solve = { version = "0.96.0", default-features = false }
109110
chalk-ir = "0.96.0"
110111
chalk-recursive = { version = "0.96.0", default-features = false }

crates/flycheck/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99
#![warn(rust_2018_idioms, unused_lifetimes)]
1010

11-
use std::{fmt, io, path::PathBuf, process::Command, time::Duration};
11+
use std::{fmt, io, process::Command, time::Duration};
1212

1313
use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
14-
use paths::{AbsPath, AbsPathBuf};
14+
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
1515
use rustc_hash::FxHashMap;
1616
use serde::Deserialize;
1717

@@ -53,7 +53,7 @@ pub enum FlycheckConfig {
5353
extra_args: Vec<String>,
5454
extra_env: FxHashMap<String, String>,
5555
ansi_color_output: bool,
56-
target_dir: Option<PathBuf>,
56+
target_dir: Option<Utf8PathBuf>,
5757
},
5858
CustomCommand {
5959
command: String,
@@ -363,7 +363,7 @@ impl FlycheckActor {
363363
});
364364

365365
cmd.arg("--manifest-path");
366-
cmd.arg(self.root.join("Cargo.toml").as_os_str());
366+
cmd.arg(self.root.join("Cargo.toml"));
367367

368368
for target in target_triples {
369369
cmd.args(["--target", target.as_str()]);

crates/ide-diagnostics/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ text-edit.workspace = true
2626
cfg.workspace = true
2727
hir.workspace = true
2828
ide-db.workspace = true
29+
paths.workspace = true
2930

3031
[dev-dependencies]
3132
expect-test = "1.4.0"

crates/ide-diagnostics/src/handlers/unlinked_file.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use ide_db::{
88
source_change::SourceChange,
99
RootDatabase,
1010
};
11+
use paths::Utf8Component;
1112
use syntax::{
1213
ast::{self, edit::IndentLevel, HasModuleItem, HasName},
1314
AstNode, TextRange,
@@ -84,10 +85,10 @@ fn fixes(ctx: &DiagnosticsContext<'_>, file_id: FileId) -> Option<Vec<Assist>> {
8485

8586
// try resolving the relative difference of the paths as inline modules
8687
let mut current = root_module;
87-
for ele in rel.as_ref().components() {
88+
for ele in rel.as_utf8_path().components() {
8889
let seg = match ele {
89-
std::path::Component::Normal(seg) => seg.to_str()?,
90-
std::path::Component::RootDir => continue,
90+
Utf8Component::Normal(seg) => seg,
91+
Utf8Component::RootDir => continue,
9192
// shouldn't occur
9293
_ => continue 'crates,
9394
};

crates/ide/src/doc_links.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ mod tests;
55

66
mod intra_doc_links;
77

8-
use std::ffi::OsStr;
9-
108
use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
119
use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options as CMarkOptions};
1210
use stdx::format_to;
@@ -134,8 +132,8 @@ pub(crate) fn remove_links(markdown: &str) -> String {
134132
pub(crate) fn external_docs(
135133
db: &RootDatabase,
136134
FilePosition { file_id, offset }: FilePosition,
137-
target_dir: Option<&OsStr>,
138-
sysroot: Option<&OsStr>,
135+
target_dir: Option<&str>,
136+
sysroot: Option<&str>,
139137
) -> Option<DocumentationLinks> {
140138
let sema = &Semantics::new(db);
141139
let file = sema.parse(file_id).syntax().clone();
@@ -331,8 +329,8 @@ fn broken_link_clone_cb(link: BrokenLink<'_>) -> Option<(CowStr<'_>, CowStr<'_>)
331329
fn get_doc_links(
332330
db: &RootDatabase,
333331
def: Definition,
334-
target_dir: Option<&OsStr>,
335-
sysroot: Option<&OsStr>,
332+
target_dir: Option<&str>,
333+
sysroot: Option<&str>,
336334
) -> DocumentationLinks {
337335
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
338336
base_url.and_then(|url| url.join(path).ok())
@@ -479,15 +477,13 @@ fn map_links<'e>(
479477
fn get_doc_base_urls(
480478
db: &RootDatabase,
481479
def: Definition,
482-
target_dir: Option<&OsStr>,
483-
sysroot: Option<&OsStr>,
480+
target_dir: Option<&str>,
481+
sysroot: Option<&str>,
484482
) -> (Option<Url>, Option<Url>) {
485483
let local_doc = target_dir
486-
.and_then(|path| path.to_str())
487484
.and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
488485
.and_then(|it| it.join("doc/").ok());
489486
let system_doc = sysroot
490-
.and_then(|it| it.to_str())
491487
.map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
492488
.and_then(|it| Url::parse(&it).ok());
493489

crates/ide/src/doc_links/tests.rs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{ffi::OsStr, iter};
1+
use std::iter;
22

33
use expect_test::{expect, Expect};
44
use hir::Semantics;
@@ -18,10 +18,10 @@ use crate::{
1818

1919
fn check_external_docs(
2020
ra_fixture: &str,
21-
target_dir: Option<&OsStr>,
21+
target_dir: Option<&str>,
2222
expect_web_url: Option<Expect>,
2323
expect_local_url: Option<Expect>,
24-
sysroot: Option<&OsStr>,
24+
sysroot: Option<&str>,
2525
) {
2626
let (analysis, position) = fixture::position(ra_fixture);
2727
let links = analysis.external_docs(position, target_dir, sysroot).unwrap();
@@ -127,10 +127,10 @@ fn external_docs_doc_builtin_type() {
127127
//- /main.rs crate:foo
128128
let x: u3$02 = 0;
129129
"#,
130-
Some(OsStr::new("/home/user/project")),
130+
Some("/home/user/project"),
131131
Some(expect![[r#"https://doc.rust-lang.org/nightly/core/primitive.u32.html"#]]),
132132
Some(expect![[r#"file:///sysroot/share/doc/rust/html/core/primitive.u32.html"#]]),
133-
Some(OsStr::new("/sysroot")),
133+
Some("/sysroot"),
134134
);
135135
}
136136

@@ -143,10 +143,10 @@ use foo$0::Foo;
143143
//- /lib.rs crate:foo
144144
pub struct Foo;
145145
"#,
146-
Some(OsStr::new("/home/user/project")),
146+
Some("/home/user/project"),
147147
Some(expect![[r#"https://docs.rs/foo/*/foo/index.html"#]]),
148148
Some(expect![[r#"file:///home/user/project/doc/foo/index.html"#]]),
149-
Some(OsStr::new("/sysroot")),
149+
Some("/sysroot"),
150150
);
151151
}
152152

@@ -157,10 +157,10 @@ fn external_docs_doc_url_std_crate() {
157157
//- /main.rs crate:std
158158
use self$0;
159159
"#,
160-
Some(OsStr::new("/home/user/project")),
160+
Some("/home/user/project"),
161161
Some(expect!["https://doc.rust-lang.org/stable/std/index.html"]),
162162
Some(expect!["file:///sysroot/share/doc/rust/html/std/index.html"]),
163-
Some(OsStr::new("/sysroot")),
163+
Some("/sysroot"),
164164
);
165165
}
166166

@@ -171,10 +171,10 @@ fn external_docs_doc_url_struct() {
171171
//- /main.rs crate:foo
172172
pub struct Fo$0o;
173173
"#,
174-
Some(OsStr::new("/home/user/project")),
174+
Some("/home/user/project"),
175175
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
176176
Some(expect![[r#"file:///home/user/project/doc/foo/struct.Foo.html"#]]),
177-
Some(OsStr::new("/sysroot")),
177+
Some("/sysroot"),
178178
);
179179
}
180180

@@ -185,10 +185,10 @@ fn external_docs_doc_url_windows_backslash_path() {
185185
//- /main.rs crate:foo
186186
pub struct Fo$0o;
187187
"#,
188-
Some(OsStr::new(r"C:\Users\user\project")),
188+
Some(r"C:\Users\user\project"),
189189
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
190190
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
191-
Some(OsStr::new("/sysroot")),
191+
Some("/sysroot"),
192192
);
193193
}
194194

@@ -199,10 +199,10 @@ fn external_docs_doc_url_windows_slash_path() {
199199
//- /main.rs crate:foo
200200
pub struct Fo$0o;
201201
"#,
202-
Some(OsStr::new(r"C:/Users/user/project")),
202+
Some("C:/Users/user/project"),
203203
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
204204
Some(expect![[r#"file:///C:/Users/user/project/doc/foo/struct.Foo.html"#]]),
205-
Some(OsStr::new("/sysroot")),
205+
Some("/sysroot"),
206206
);
207207
}
208208

crates/ide/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,6 @@ mod view_item_tree;
5858
mod view_memory_layout;
5959
mod view_mir;
6060

61-
use std::ffi::OsStr;
62-
6361
use cfg::CfgOptions;
6462
use fetch_crates::CrateInfo;
6563
use hir::ChangeWithProcMacros;
@@ -511,8 +509,8 @@ impl Analysis {
511509
pub fn external_docs(
512510
&self,
513511
position: FilePosition,
514-
target_dir: Option<&OsStr>,
515-
sysroot: Option<&OsStr>,
512+
target_dir: Option<&str>,
513+
sysroot: Option<&str>,
516514
) -> Cancellable<doc_links::DocumentationLinks> {
517515
self.with_db(|db| {
518516
doc_links::external_docs(db, position, target_dir, sysroot).unwrap_or_default()

crates/load-cargo/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ tracing.workspace = true
2020

2121
hir-expand.workspace = true
2222
ide-db.workspace = true
23+
paths.workspace = true
2324
proc-macro-api.workspace = true
2425
project-model.workspace = true
2526
span.workspace = true

crates/load-cargo/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub fn load_workspace_at(
3838
load_config: &LoadCargoConfig,
3939
progress: &dyn Fn(String),
4040
) -> anyhow::Result<(RootDatabase, vfs::Vfs, Option<ProcMacroServer>)> {
41-
let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
41+
let root = AbsPathBuf::assert_utf8(std::env::current_dir()?.join(root));
4242
let root = ProjectManifest::discover_single(&root)?;
4343
let mut workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
4444

crates/paths/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,14 @@ rust-version.workspace = true
1212
doctest = false
1313

1414
[dependencies]
15+
camino.workspace = true
1516
# Adding this dep sadly puts a lot of rust-analyzer crates after the
1617
# serde-derive crate. Even though we don't activate the derive feature here,
1718
# someone else in the crate graph certainly does!
1819
# serde.workspace = true
1920

21+
[features]
22+
serde1 = ["camino/serde1"]
23+
2024
[lints]
21-
workspace = true
25+
workspace = true

0 commit comments

Comments
 (0)