Skip to content

Commit

Permalink
internal: Enforce utf8 paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Veykril committed Mar 19, 2024
1 parent ba33959 commit 399dbc0
Show file tree
Hide file tree
Showing 46 changed files with 380 additions and 316 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ anyhow = "1.0.75"
arrayvec = "0.7.4"
bitflags = "2.4.1"
cargo_metadata = "0.18.1"
camino = "1.1.6"
chalk-solve = { version = "0.96.0", default-features = false }
chalk-ir = "0.96.0"
chalk-recursive = { version = "0.96.0", default-features = false }
Expand Down
8 changes: 4 additions & 4 deletions crates/flycheck/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@

#![warn(rust_2018_idioms, unused_lifetimes)]

use std::{fmt, io, path::PathBuf, process::Command, time::Duration};
use std::{fmt, io, process::Command, time::Duration};

use crossbeam_channel::{never, select, unbounded, Receiver, Sender};
use paths::{AbsPath, AbsPathBuf};
use paths::{AbsPath, AbsPathBuf, Utf8PathBuf};
use rustc_hash::FxHashMap;
use serde::Deserialize;

Expand Down Expand Up @@ -53,7 +53,7 @@ pub enum FlycheckConfig {
extra_args: Vec<String>,
extra_env: FxHashMap<String, String>,
ansi_color_output: bool,
target_dir: Option<PathBuf>,
target_dir: Option<Utf8PathBuf>,
},
CustomCommand {
command: String,
Expand Down Expand Up @@ -363,7 +363,7 @@ impl FlycheckActor {
});

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

for target in target_triples {
cmd.args(["--target", target.as_str()]);
Expand Down
1 change: 1 addition & 0 deletions crates/ide-diagnostics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ text-edit.workspace = true
cfg.workspace = true
hir.workspace = true
ide-db.workspace = true
paths.workspace = true

[dev-dependencies]
expect-test = "1.4.0"
Expand Down
7 changes: 4 additions & 3 deletions crates/ide-diagnostics/src/handlers/unlinked_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use ide_db::{
source_change::SourceChange,
RootDatabase,
};
use paths::Utf8Component;
use syntax::{
ast::{self, edit::IndentLevel, HasModuleItem, HasName},
AstNode, TextRange,
Expand Down Expand Up @@ -84,10 +85,10 @@ fn fixes(ctx: &DiagnosticsContext<'_>, file_id: FileId) -> Option<Vec<Assist>> {

// try resolving the relative difference of the paths as inline modules
let mut current = root_module;
for ele in rel.as_ref().components() {
for ele in rel.as_utf8_path().components() {
let seg = match ele {
std::path::Component::Normal(seg) => seg.to_str()?,
std::path::Component::RootDir => continue,
Utf8Component::Normal(seg) => seg,
Utf8Component::RootDir => continue,
// shouldn't occur
_ => continue 'crates,
};
Expand Down
16 changes: 6 additions & 10 deletions crates/ide/src/doc_links.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ mod tests;

mod intra_doc_links;

use std::ffi::OsStr;

use pulldown_cmark::{BrokenLink, CowStr, Event, InlineStr, LinkType, Options, Parser, Tag};
use pulldown_cmark_to_cmark::{cmark_resume_with_options, Options as CMarkOptions};
use stdx::format_to;
Expand Down Expand Up @@ -134,8 +132,8 @@ pub(crate) fn remove_links(markdown: &str) -> String {
pub(crate) fn external_docs(
db: &RootDatabase,
FilePosition { file_id, offset }: FilePosition,
target_dir: Option<&OsStr>,
sysroot: Option<&OsStr>,
target_dir: Option<&str>,
sysroot: Option<&str>,
) -> Option<DocumentationLinks> {
let sema = &Semantics::new(db);
let file = sema.parse(file_id).syntax().clone();
Expand Down Expand Up @@ -331,8 +329,8 @@ fn broken_link_clone_cb(link: BrokenLink<'_>) -> Option<(CowStr<'_>, CowStr<'_>)
fn get_doc_links(
db: &RootDatabase,
def: Definition,
target_dir: Option<&OsStr>,
sysroot: Option<&OsStr>,
target_dir: Option<&str>,
sysroot: Option<&str>,
) -> DocumentationLinks {
let join_url = |base_url: Option<Url>, path: &str| -> Option<Url> {
base_url.and_then(|url| url.join(path).ok())
Expand Down Expand Up @@ -479,15 +477,13 @@ fn map_links<'e>(
fn get_doc_base_urls(
db: &RootDatabase,
def: Definition,
target_dir: Option<&OsStr>,
sysroot: Option<&OsStr>,
target_dir: Option<&str>,
sysroot: Option<&str>,
) -> (Option<Url>, Option<Url>) {
let local_doc = target_dir
.and_then(|path| path.to_str())
.and_then(|path| Url::parse(&format!("file:///{path}/")).ok())
.and_then(|it| it.join("doc/").ok());
let system_doc = sysroot
.and_then(|it| it.to_str())
.map(|sysroot| format!("file:///{sysroot}/share/doc/rust/html/"))
.and_then(|it| Url::parse(&it).ok());

Expand Down
30 changes: 15 additions & 15 deletions crates/ide/src/doc_links/tests.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{ffi::OsStr, iter};
use std::iter;

use expect_test::{expect, Expect};
use hir::Semantics;
Expand All @@ -18,10 +18,10 @@ use crate::{

fn check_external_docs(
ra_fixture: &str,
target_dir: Option<&OsStr>,
target_dir: Option<&str>,
expect_web_url: Option<Expect>,
expect_local_url: Option<Expect>,
sysroot: Option<&OsStr>,
sysroot: Option<&str>,
) {
let (analysis, position) = fixture::position(ra_fixture);
let links = analysis.external_docs(position, target_dir, sysroot).unwrap();
Expand Down Expand Up @@ -127,10 +127,10 @@ fn external_docs_doc_builtin_type() {
//- /main.rs crate:foo
let x: u3$02 = 0;
"#,
Some(OsStr::new("/home/user/project")),
Some("/home/user/project"),
Some(expect![[r#"https://doc.rust-lang.org/nightly/core/primitive.u32.html"#]]),
Some(expect![[r#"file:///sysroot/share/doc/rust/html/core/primitive.u32.html"#]]),
Some(OsStr::new("/sysroot")),
Some("/sysroot"),
);
}

Expand All @@ -143,10 +143,10 @@ use foo$0::Foo;
//- /lib.rs crate:foo
pub struct Foo;
"#,
Some(OsStr::new("/home/user/project")),
Some("/home/user/project"),
Some(expect![[r#"https://docs.rs/foo/*/foo/index.html"#]]),
Some(expect![[r#"file:///home/user/project/doc/foo/index.html"#]]),
Some(OsStr::new("/sysroot")),
Some("/sysroot"),
);
}

Expand All @@ -157,10 +157,10 @@ fn external_docs_doc_url_std_crate() {
//- /main.rs crate:std
use self$0;
"#,
Some(OsStr::new("/home/user/project")),
Some("/home/user/project"),
Some(expect!["https://doc.rust-lang.org/stable/std/index.html"]),
Some(expect!["file:///sysroot/share/doc/rust/html/std/index.html"]),
Some(OsStr::new("/sysroot")),
Some("/sysroot"),
);
}

Expand All @@ -171,10 +171,10 @@ fn external_docs_doc_url_struct() {
//- /main.rs crate:foo
pub struct Fo$0o;
"#,
Some(OsStr::new("/home/user/project")),
Some("/home/user/project"),
Some(expect![[r#"https://docs.rs/foo/*/foo/struct.Foo.html"#]]),
Some(expect![[r#"file:///home/user/project/doc/foo/struct.Foo.html"#]]),
Some(OsStr::new("/sysroot")),
Some("/sysroot"),
);
}

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

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

Expand Down
6 changes: 2 additions & 4 deletions crates/ide/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ mod view_item_tree;
mod view_memory_layout;
mod view_mir;

use std::ffi::OsStr;

use cfg::CfgOptions;
use fetch_crates::CrateInfo;
use hir::ChangeWithProcMacros;
Expand Down Expand Up @@ -511,8 +509,8 @@ impl Analysis {
pub fn external_docs(
&self,
position: FilePosition,
target_dir: Option<&OsStr>,
sysroot: Option<&OsStr>,
target_dir: Option<&str>,
sysroot: Option<&str>,
) -> Cancellable<doc_links::DocumentationLinks> {
self.with_db(|db| {
doc_links::external_docs(db, position, target_dir, sysroot).unwrap_or_default()
Expand Down
1 change: 1 addition & 0 deletions crates/load-cargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tracing.workspace = true

hir-expand.workspace = true
ide-db.workspace = true
paths.workspace = true
proc-macro-api.workspace = true
project-model.workspace = true
span.workspace = true
Expand Down
2 changes: 1 addition & 1 deletion crates/load-cargo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub fn load_workspace_at(
load_config: &LoadCargoConfig,
progress: &dyn Fn(String),
) -> anyhow::Result<(RootDatabase, vfs::Vfs, Option<ProcMacroServer>)> {
let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
let root = AbsPathBuf::assert_utf8(std::env::current_dir()?.join(root));
let root = ProjectManifest::discover_single(&root)?;
let mut workspace = ProjectWorkspace::load(root, cargo_config, progress)?;

Expand Down
6 changes: 5 additions & 1 deletion crates/paths/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ rust-version.workspace = true
doctest = false

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

[features]
serde1 = ["camino/serde1"]

[lints]
workspace = true
workspace = true
Loading

0 comments on commit 399dbc0

Please sign in to comment.