dsherret / ts-morph

TypeScript Compiler API wrapper for static analysis and programmatic code changes.
https://ts-morph.com
MIT License
5k stars 195 forks source link

useInMemoryFileSystem: true and addSourceFilesAtPaths('**/*.*') doesn't work #1542

Closed radzserg closed 5 months ago

radzserg commented 5 months ago

Describe the bug

Version: 22.0.0

To Reproduce


 // Initialize the project with in-memory file system
  const project = new Project({
    useInMemoryFileSystem: true,
  });

  // Create source files directly in the in-memory file system
  project.createSourceFile('/src/test.ts', 'const a = 12;');

  // List all source files in the project
  const sourceFiles = project.getSourceFiles();
  for (const sourceFile of sourceFiles) {
    console.log(sourceFile.getFilePath());
  }

  // Add source files using a glob pattern
  const addedFiles = project.addSourceFilesAtPaths('**/*.*');
  console.log(
    'files',
    addedFiles.map((file) => file.getFilePath()),
  );

/*
Outputs:
/src/test.ts
files []
*/

/src/test.ts has been added to the project but addSourceFilesAtPaths('**/*.*') cannot find it.

Expected behavior

addSourceFilesAtPaths('**/*.*') should find created file.

dsherret commented 5 months ago

You need to save the file to the file system (sourceFile.saveSync() or project.saveSync())

radzserg commented 5 months ago

Thank you @dsherret. That's a great package. Appreciate your hard work 👍