@@ -285,6 +285,87 @@ namespace vix::commands::RunCommand::detail
285285 return false ;
286286 }
287287
288+ std::vector<std::string> extract_script_include_prefixes_for_autodeps (const fs::path &cppPath)
289+ {
290+ std::vector<std::string> prefixes;
291+
292+ std::ifstream ifs (cppPath);
293+ if (!ifs)
294+ return prefixes;
295+
296+ std::string line;
297+ while (std::getline (ifs, line))
298+ {
299+ const auto incPos = line.find (" #include" );
300+ if (incPos == std::string::npos)
301+ continue ;
302+
303+ std::size_t start = line.find (' <' , incPos);
304+ char closer = ' >' ;
305+
306+ if (start == std::string::npos)
307+ {
308+ start = line.find (' "' , incPos);
309+ closer = ' "' ;
310+ }
311+
312+ if (start == std::string::npos)
313+ continue ;
314+
315+ const std::size_t end = line.find (closer, start + 1 );
316+ if (end == std::string::npos || end <= start + 1 )
317+ continue ;
318+
319+ std::string inc = line.substr (start + 1 , end - start - 1 );
320+ if (inc.empty ())
321+ continue ;
322+
323+ const auto slash = inc.find (' /' );
324+ const std::string prefix = slash == std::string::npos
325+ ? inc
326+ : inc.substr (0 , slash);
327+
328+ if (!prefix.empty ())
329+ prefixes.push_back (prefix);
330+ }
331+
332+ std::sort (prefixes.begin (), prefixes.end ());
333+ prefixes.erase (std::unique (prefixes.begin (), prefixes.end ()), prefixes.end ());
334+
335+ return prefixes;
336+ }
337+
338+ bool script_may_use_dep_id (const std::vector<std::string> &includePrefixes,
339+ const std::string &depId)
340+ {
341+ if (includePrefixes.empty () || depId.empty ())
342+ return false ;
343+
344+ std::string normalized = depId;
345+ normalized.erase (std::remove (normalized.begin (), normalized.end (), ' @' ), normalized.end ());
346+
347+ const auto slash = normalized.find (' /' );
348+ const std::string namespacePart =
349+ slash == std::string::npos ? normalized : normalized.substr (0 , slash);
350+
351+ const std::string packagePart =
352+ slash == std::string::npos ? normalized : normalized.substr (slash + 1 );
353+
354+ for (const auto &prefix : includePrefixes)
355+ {
356+ if (prefix == normalized)
357+ return true ;
358+
359+ if (prefix == namespacePart)
360+ return true ;
361+
362+ if (prefix == packagePart)
363+ return true ;
364+ }
365+
366+ return false ;
367+ }
368+
288369 void apply_auto_deps_includes_from_deps_folder (Options &opt, const fs::path &startDir)
289370 {
290371 auto already_has_I = [&](const std::string &inc) -> bool
@@ -333,6 +414,9 @@ namespace vix::commands::RunCommand::detail
333414
334415 std::unordered_set<std::string> localPkgDirs;
335416
417+ const std::vector<std::string> scriptIncludePrefixes =
418+ extract_script_include_prefixes_for_autodeps (startDir / opt.cppFile .filename ());
419+
336420 auto scan_one = [&](const fs::path &baseDir)
337421 {
338422 const fs::path depsRoot = baseDir / " .vix" / " deps" ;
@@ -389,6 +473,10 @@ namespace vix::commands::RunCommand::detail
389473 continue ;
390474
391475 const std::string id = item[" id" ].get <std::string>();
476+
477+ if (!script_may_use_dep_id (scriptIncludePrefixes, id))
478+ continue ;
479+
392480 const std::string pkgDir = dep_id_to_dir_local (id);
393481
394482 if (localPkgDirs.contains (pkgDir))
0 commit comments