@@ -65,59 +65,26 @@ pub fn template_from_artifact_and_subpath(
65
65
] )
66
66
}
67
67
68
- /// Check if a directory looks like a Tangram artifacts directory by examining its contents.
69
- /// Returns true if the directory contains at least one entry that parses as a valid Tangram ID.
70
- pub fn looks_like_artifacts_dir ( path : & std:: path:: Path ) -> bool {
71
- let Ok ( entries) = std:: fs:: read_dir ( path) else {
72
- return false ;
73
- } ;
74
-
75
- entries. filter_map ( Result :: ok) . any ( |entry| {
76
- let name = entry. file_name ( ) ;
77
- name. to_str ( )
78
- . and_then ( |s| s. parse :: < tg:: Id > ( ) . ok ( ) )
79
- . is_some ( )
80
- } )
81
- }
82
-
83
- /// Find the artifacts directory by searching up from the given path.
84
- /// On Linux, falls back to /.tangram/artifacts when reaching the root.
85
- pub fn find_artifacts_dir ( start_path : & std:: path:: Path ) -> tg:: Result < PathBuf > {
86
- for path in start_path. ancestors ( ) {
87
- let directory = path. join ( "artifacts" ) ;
88
- if directory. exists ( ) && looks_like_artifacts_dir ( & directory) {
89
- return Ok ( directory) ;
90
- }
91
-
92
- // On Linux, when we reach the root, check /.tangram/artifacts (chroot path)
93
- #[ cfg( target_os = "linux" ) ]
94
- if path == std:: path:: Path :: new ( "/" ) {
95
- let directory = path. join ( ".tangram/artifacts" ) ;
96
- if directory. exists ( ) && looks_like_artifacts_dir ( & directory) {
97
- return Ok ( directory) ;
98
- }
99
- }
100
- }
101
- Err ( tg:: error!( "failed to find artifacts directory" ) )
102
- }
103
-
104
68
/// Compute the closest located artifact path for the current running process, reusing the result for subsequent lookups.
105
69
pub static CLOSEST_ARTIFACT_PATH : LazyLock < String > = LazyLock :: new ( || {
70
+ let mut closest_artifact_path = None ;
106
71
let cwd = std:: env:: current_exe ( )
107
72
. expect ( "Failed to get the current directory" )
108
73
. canonicalize ( )
109
74
. expect ( "failed to canonicalize current directory" ) ;
110
-
111
- let parent = cwd
112
- . parent ( )
113
- . expect ( "executable should have a parent directory" ) ;
114
- let artifacts_dir =
115
- find_artifacts_dir ( parent) . expect ( "Failed to find the closest artifact path" ) ;
116
-
117
- artifacts_dir
118
- . to_str ( )
119
- . expect ( "artifacts directory should be valid UTF-8" )
120
- . to_string ( )
75
+ for path in cwd. ancestors ( ) . skip ( 1 ) {
76
+ let directory = path. join ( ".tangram/artifacts" ) ;
77
+ if directory. exists ( ) {
78
+ closest_artifact_path = Some (
79
+ directory
80
+ . to_str ( )
81
+ . expect ( "artifacts directory should be valid UTF-8" )
82
+ . to_string ( ) ,
83
+ ) ;
84
+ break ;
85
+ }
86
+ }
87
+ closest_artifact_path. expect ( "Failed to find the closest artifact path" )
121
88
} ) ;
122
89
123
90
/// Render a [`tg::template::Data`] to a `String` using the closest located artifact path.
0 commit comments