Skip to content

Commit 1f9cba1

Browse files
authored
Revert "fix(forge doc): use relative path instead of full path (#12373)"
This reverts commit 118e12e.
1 parent 3d2afe0 commit 1f9cba1

File tree

4 files changed

+18
-55
lines changed

4 files changed

+18
-55
lines changed

crates/doc/src/builder.rs

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

126126
let out_dir = self.out_dir()?;
127-
let out_target_dir = out_dir.clone();
128127
let documents = compiler.enter_mut(|compiler| -> eyre::Result<Vec<Vec<Document>>> {
129128
let gcx = compiler.gcx();
130129
let documents = combined_sources
@@ -198,7 +197,7 @@ impl DocBuilder {
198197
path.clone(),
199198
target_path,
200199
from_library,
201-
out_target_dir.clone(),
200+
self.config.out.clone(),
202201
)
203202
.with_content(DocumentContent::Single(item), ident))
204203
})
@@ -232,7 +231,7 @@ impl DocBuilder {
232231
path.clone(),
233232
target_path,
234233
from_library,
235-
out_target_dir.clone(),
234+
self.config.out.clone(),
236235
)
237236
.with_content(DocumentContent::Constants(consts), identity),
238237
)
@@ -251,7 +250,7 @@ impl DocBuilder {
251250
path.clone(),
252251
target_path,
253252
from_library,
254-
out_target_dir.clone(),
253+
self.config.out.clone(),
255254
)
256255
.with_content(
257256
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.relative_output_path().to_path_buf());
65+
return Some(candidate.target_path.clone());
6666
}
6767
}
6868
None

crates/doc/src/writer/as_doc.rs

Lines changed: 14 additions & 2 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;
12+
use std::path::{Path, PathBuf};
1313

1414
/// The result of [`AsDoc::as_doc`].
1515
pub type AsDocResult = Result<String, std::fmt::Error>;
@@ -141,6 +141,9 @@ 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+
144147
let mut bases = vec![];
145148
let linked =
146149
read_context!(self, CONTRACT_INHERITANCE_ID, ContractInheritance);
@@ -152,7 +155,11 @@ impl AsDoc for Document {
152155
.as_ref()
153156
.and_then(|link| {
154157
link.get(base_ident).map(|path| {
155-
let path = Path::new("/").join(path);
158+
let path = Path::new("/").join(
159+
path.strip_prefix(&src_target_dir)
160+
.ok()
161+
.unwrap_or(path),
162+
);
156163
Markdown::Link(&base_doc, &path.display().to_string())
157164
.as_doc()
158165
})
@@ -280,6 +287,11 @@ impl AsDoc for Document {
280287
}
281288

282289
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+
283295
/// Writes a function to the buffer.
284296
fn write_function(
285297
&self,

crates/forge/tests/cli/doc.rs

Lines changed: 0 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -69,51 +69,3 @@ 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/"),
110-
"Hyperlink should use relative path starting with /src/, but found: {:?}",
111-
content.lines().find(|line| line.contains("[IBase]")).unwrap_or("not found")
112-
);
113-
assert!(!content.contains("/Users/"), "Hyperlink should not contain absolute path /Users/");
114-
assert!(!content.contains("/home/"), "Hyperlink should not contain absolute path /home/");
115-
assert!(
116-
content.contains("IBase.sol/interface.IBase.md"),
117-
"Hyperlink should point to IBase.sol/interface.IBase.md"
118-
);
119-
});

0 commit comments

Comments
 (0)