Skip to content

Commit eb76d29

Browse files
authored
fix(forge doc): use relative path instead of full path (#12519)
* Revert "Revert "fix(forge doc): use relative path instead of full path" (#12477)" This reverts commit ebcd59b. * fix win and test
1 parent 388c9c7 commit eb76d29

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

crates/doc/src/builder.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ impl DocBuilder {
124124
.collect::<Vec<_>>();
125125

126126
let out_dir = self.out_dir()?;
127+
let out_target_dir = out_dir.clone();
127128
let documents = compiler.enter_mut(|compiler| -> eyre::Result<Vec<Vec<Document>>> {
128129
let gcx = compiler.gcx();
129130
let documents = combined_sources
@@ -197,7 +198,7 @@ impl DocBuilder {
197198
path.clone(),
198199
target_path,
199200
from_library,
200-
self.config.out.clone(),
201+
out_target_dir.clone(),
201202
)
202203
.with_content(DocumentContent::Single(item), ident))
203204
})
@@ -231,7 +232,7 @@ impl DocBuilder {
231232
path.clone(),
232233
target_path,
233234
from_library,
234-
self.config.out.clone(),
235+
out_target_dir.clone(),
235236
)
236237
.with_content(DocumentContent::Constants(consts), identity),
237238
)
@@ -250,7 +251,7 @@ impl DocBuilder {
250251
path.clone(),
251252
target_path,
252253
from_library,
253-
self.config.out.clone(),
254+
out_target_dir.clone(),
254255
)
255256
.with_content(
256257
DocumentContent::OverloadedFunctions(funcs),

crates/doc/src/preprocessor/contract_inheritance.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl ContractInheritance {
6262
&& let ParseSource::Contract(ref contract) = item.source
6363
&& base == contract.name.safe_unwrap().name
6464
{
65-
return Some(candidate.target_path.clone());
65+
return Some(candidate.relative_output_path().to_path_buf());
6666
}
6767
}
6868
None

crates/doc/src/writer/as_doc.rs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::{
99
};
1010
use itertools::Itertools;
1111
use solang_parser::pt::{Base, FunctionDefinition};
12-
use std::path::{Path, PathBuf};
12+
use std::path::Path;
1313

1414
/// The result of [`AsDoc::as_doc`].
1515
pub type AsDocResult = Result<String, std::fmt::Error>;
@@ -141,9 +141,6 @@ impl AsDoc for Document {
141141
if !contract.base.is_empty() {
142142
writer.write_bold("Inherits:")?;
143143

144-
// we need this to find the _relative_ paths
145-
let src_target_dir = self.target_src_dir();
146-
147144
let mut bases = vec![];
148145
let linked =
149146
read_context!(self, CONTRACT_INHERITANCE_ID, ContractInheritance);
@@ -155,11 +152,11 @@ impl AsDoc for Document {
155152
.as_ref()
156153
.and_then(|link| {
157154
link.get(base_ident).map(|path| {
158-
let path = Path::new("/").join(
159-
path.strip_prefix(&src_target_dir)
160-
.ok()
161-
.unwrap_or(path),
162-
);
155+
let path = if cfg!(windows) {
156+
Path::new("\\").join(path)
157+
} else {
158+
Path::new("/").join(path)
159+
};
163160
Markdown::Link(&base_doc, &path.display().to_string())
164161
.as_doc()
165162
})
@@ -287,11 +284,6 @@ impl AsDoc for Document {
287284
}
288285

289286
impl Document {
290-
/// Where all the source files are written to
291-
fn target_src_dir(&self) -> PathBuf {
292-
self.out_target_dir.join("src")
293-
}
294-
295287
/// Writes a function to the buffer.
296288
fn write_function(
297289
&self,

crates/forge/tests/cli/doc.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,46 @@ contract Example is IExample {
6969
assert!(content.contains("Process multiple addresses"));
7070
assert!(content.contains("Process an address with a value"));
7171
});
72+
73+
// Test that hyperlinks use relative paths, not absolute paths
74+
// fixes <https://github.com/foundry-rs/foundry/issues/12361>
75+
forgetest_init!(hyperlinks_use_relative_paths, |prj, cmd| {
76+
prj.add_source(
77+
"IBase.sol",
78+
r#"
79+
// SPDX-License-Identifier: MIT
80+
pragma solidity ^0.8.0;
81+
82+
interface IBase {
83+
function baseFunction() external;
84+
}
85+
"#,
86+
);
87+
88+
prj.add_source(
89+
"Derived.sol",
90+
r#"
91+
// SPDX-License-Identifier: MIT
92+
pragma solidity ^0.8.0;
93+
94+
import "./IBase.sol";
95+
96+
/// @dev Inherits: {IBase}
97+
contract Derived is IBase {
98+
function baseFunction() external override {}
99+
}
100+
"#,
101+
);
102+
103+
cmd.args(["doc", "--build"]).assert_success();
104+
105+
let doc_path = prj.root().join("docs/src/src/Derived.sol/contract.Derived.md");
106+
let content = std::fs::read_to_string(&doc_path).unwrap();
107+
108+
assert!(
109+
content.contains("[IBase](/src/IBase.sol/interface.IBase.md")
110+
|| content.contains("[IBase](\\src\\IBase.sol\\interface.IBase.md"),
111+
"Hyperlink should use relative path but found: {:?}",
112+
content.lines().find(|line| line.contains("[IBase]")).unwrap_or("not found")
113+
);
114+
});

0 commit comments

Comments
 (0)