11use std:: ffi:: OsStr ;
2- use std:: hash:: Hash ;
32use std:: path:: Path ;
43
54use anyhow:: Context ;
65use relative_path:: RelativePath ;
7- use rnix:: SyntaxNode ;
86use rowan:: ast:: AstNode ;
97
10- use crate :: nix_file:: { NixFile , ResolvedPath } ;
11- use crate :: problem:: { npv_121, npv_122, npv_123, npv_124, npv_125, npv_126, npv_169 } ;
8+ use crate :: nix_file:: ResolvedPath ;
9+ use crate :: problem:: { npv_121, npv_122, npv_123, npv_124, npv_125, npv_126} ;
1210use crate :: structure:: read_dir_sorted;
1311use crate :: validation:: { self , ResultIteratorExt , Validation :: Success } ;
1412use crate :: NixFileStore ;
@@ -113,75 +111,6 @@ fn check_path(
113111 } )
114112}
115113
116- fn check_nix_path (
117- node : & SyntaxNode ,
118- nix_file : & NixFile ,
119- relative_package_dir : & RelativePath ,
120- absolute_package_dir : & Path ,
121- subpath : & RelativePath ,
122- ) -> validation:: Validation < ( ) > {
123- let line = nix_file. line_index . line ( node. text_range ( ) . start ( ) . into ( ) ) ;
124- let text = node. text ( ) . to_string ( ) ;
125- // We're only interested in Path expressions
126- let Some ( path) = rnix:: ast:: Path :: cast ( node. clone ( ) ) else {
127- return Success ( ( ) ) ;
128- } ;
129-
130- match nix_file. static_resolve_path ( & path, absolute_package_dir) {
131- ResolvedPath :: Interpolated => npv_121:: NixFileContainsPathInterpolation :: new (
132- relative_package_dir,
133- subpath,
134- line,
135- text,
136- )
137- . into ( ) ,
138- ResolvedPath :: SearchPath => {
139- npv_122:: NixFileContainsSearchPath :: new ( relative_package_dir, subpath, line, text)
140- . into ( )
141- }
142- ResolvedPath :: Outside => npv_123:: NixFileContainsPathOutsideDirectory :: new (
143- relative_package_dir,
144- subpath,
145- line,
146- text,
147- )
148- . into ( ) ,
149- ResolvedPath :: Unresolvable ( err) => npv_124:: NixFileContainsUnresolvablePath :: new (
150- relative_package_dir,
151- subpath,
152- line,
153- text,
154- err,
155- )
156- . into ( ) ,
157- ResolvedPath :: Within ( ..) => {
158- // No need to handle the case of it being inside the directory, since we scan
159- // through the entire directory recursively in any case.
160- Success ( ( ) )
161- }
162- }
163- }
164- fn check_nix_with (
165- node : & SyntaxNode ,
166- nix_file : & NixFile ,
167- _relative_package_dir : & RelativePath ,
168- _absolute_package_dir : & Path ,
169- subpath : & RelativePath ,
170- ) -> validation:: Validation < ( ) > {
171- let _line = nix_file. line_index . line ( node. text_range ( ) . start ( ) . into ( ) ) ;
172- let _text = node. text ( ) . to_string ( ) ;
173- // We're only interested in Path expressions
174- let Some ( with) = rnix:: ast:: With :: cast ( node. clone ( ) ) else {
175- return Success ( ( ) ) ;
176- } ;
177- // let Some(with_body) = with.body() else {
178- // return Success(());
179- // };
180- // println!("{}", with_body.syntax());
181-
182- npv_169:: TopLevelWithMayShadowVariablesAndBreakStaticChecks :: new ( subpath) . into ( )
183- }
184-
185114/// Check whether a Nix file contains path expression references pointing outside the package
186115/// directory.
187116fn check_nix_file (
@@ -196,27 +125,50 @@ fn check_nix_file(
196125
197126 Ok ( validation:: sequence_ (
198127 nix_file. syntax_root . syntax ( ) . descendants ( ) . map ( |node| {
199- match check_nix_path (
200- & node,
201- nix_file,
202- relative_package_dir,
203- absolute_package_dir,
204- subpath,
205- ) {
206- Success ( ( ) ) => Success ( ( ) ) ,
207- value => return value,
208- } ;
209- match check_nix_with (
210- & node,
211- nix_file,
212- relative_package_dir,
213- absolute_package_dir,
214- subpath,
215- ) {
216- Success ( ( ) ) => Success ( ( ) ) ,
217- value => return value,
128+ let line = nix_file. line_index . line ( node. text_range ( ) . start ( ) . into ( ) ) ;
129+ let text = node. text ( ) . to_string ( ) ;
130+
131+ // We're only interested in Path expressions
132+ let Some ( path) = rnix:: ast:: Path :: cast ( node) else {
133+ return Success ( ( ) ) ;
218134 } ;
219- Success ( ( ) )
135+
136+ match nix_file. static_resolve_path ( & path, absolute_package_dir) {
137+ ResolvedPath :: Interpolated => npv_121:: NixFileContainsPathInterpolation :: new (
138+ relative_package_dir,
139+ subpath,
140+ line,
141+ text,
142+ )
143+ . into ( ) ,
144+ ResolvedPath :: SearchPath => npv_122:: NixFileContainsSearchPath :: new (
145+ relative_package_dir,
146+ subpath,
147+ line,
148+ text,
149+ )
150+ . into ( ) ,
151+ ResolvedPath :: Outside => npv_123:: NixFileContainsPathOutsideDirectory :: new (
152+ relative_package_dir,
153+ subpath,
154+ line,
155+ text,
156+ )
157+ . into ( ) ,
158+ ResolvedPath :: Unresolvable ( err) => npv_124:: NixFileContainsUnresolvablePath :: new (
159+ relative_package_dir,
160+ subpath,
161+ line,
162+ text,
163+ err,
164+ )
165+ . into ( ) ,
166+ ResolvedPath :: Within ( ..) => {
167+ // No need to handle the case of it being inside the directory, since we scan
168+ // through the entire directory recursively in any case.
169+ Success ( ( ) )
170+ }
171+ }
220172 } ) ,
221173 ) )
222174}
0 commit comments