@@ -69,8 +69,14 @@ impl File {
6969 Some ( package) if is_testthat_file ( self , db) => {
7070 testthat_imports ( self , db, package, None )
7171 } ,
72- Some ( package) => package_imports ( self , db, package) ,
73- None => script_imports ( self , db) ,
72+ Some ( package) if is_package_source ( self , db, package) => {
73+ package_imports ( self , db, package)
74+ } ,
75+ // A file with a package back-pointer that isn't a loadable `R/`
76+ // file (`data-raw/`, `inst/`, a non-collated `R/` file) lives in
77+ // the package but isn't loaded with it. Resolve it as a standalone
78+ // script, same as a file with no package at all.
79+ _ => script_imports ( self , db) ,
7480 }
7581 }
7682
@@ -113,12 +119,25 @@ impl File {
113119 Some ( package) if is_testthat_file ( self , db) => {
114120 testthat_imports ( self , db, package, Some ( offset) )
115121 } ,
116- Some ( package) => narrow_package_top_level ( self , db, package) ,
117- None => narrow_script_top_level ( self , db, offset) ,
122+ Some ( package) if is_package_source ( self , db, package) => {
123+ narrow_package_top_level ( self , db, package)
124+ } ,
125+ // A packaged file that isn't a loadable `R/` file narrows like a
126+ // standalone script.
127+ _ => narrow_script_top_level ( self , db, offset) ,
118128 }
119129 }
120130}
121131
132+ /// Whether `file` is one of its package's loadable `R/` files, the ones in
133+ /// `package.files()`. A file can carry a package back-pointer without being
134+ /// loadable: `data-raw/`, `inst/`, and `R/` files left out of a `Collate:`
135+ /// directive all land in `package.scripts()` instead and resolve as
136+ /// standalone scripts.
137+ fn is_package_source ( file : File , db : & dyn Db , package : Package ) -> bool {
138+ package. files ( db) . contains ( & file)
139+ }
140+
122141fn narrow_script_top_level ( file : File , db : & dyn Db , offset : TextSize ) -> Vec < ImportLayer > {
123142 let mut layers = attach_layers ( file, db, Some ( offset) ) ;
124143 extend_with_default_search_path ( db, & mut layers) ;
0 commit comments