@@ -61,7 +61,7 @@ Future<(Isolate, SendPort)> createReporter(
6161 final Directory tempDir = Directory .systemTemp.createTempSync ();
6262
6363 final (customImportLine, method, hasArgs) =
64- await getCustomReporterImport (as : 'e1' ) ?? ('' , '' , false );
64+ await getCustomReporterImport (as : 'e1' ,path : tempDir.path ) ?? ('' , '' , false );
6565 final importLine = customImportLine.isEmpty
6666 ? switch (reporter) {
6767 String package when package.contains ('/' ) =>
@@ -109,8 +109,10 @@ Future<void> main(List<String> args, SendPort sendPort) async {
109109 );
110110}
111111''' ;
112-
113- final File tempFile = File (p.join (tempDir.path, '_reporter_temp_.dart' ));
112+ String absoluteFilePath = File (p.join (tempDir.path, '_reporter_temp_.dart' )).absolute.path;
113+ /// Ensure the parent directory exists
114+ Directory (p.dirname (absoluteFilePath)).createSync (recursive: true );
115+ final File tempFile = File (absoluteFilePath);
114116 await tempFile.writeAsString (code);
115117
116118 final ReceivePort receivePort = ReceivePort ();
@@ -137,10 +139,13 @@ Future<void> main(List<String> args, SendPort sendPort) async {
137139}
138140
139141Future <(String , String , bool )?> getCustomReporterImport (
140- {String as = '' }) async {
142+ {String as = '' , String path = '' }) async {
141143 final customReporter = File (p.join ('test' , 'reporter.dart' ));
144+ String relativePath = _posixRelative (customReporter.absolute.path, from: path);
145+ /// Get the absolute path and replace '\' with '\\'
146+ final sanitizedPath = relativePath.replaceAll (r'\' , r'\\' );
142147 final customReporterImportLine =
143- "import '${ customReporter . absolute . path } '${as .isNotEmpty ? 'as $as ' : '' };" ;
148+ "import '$sanitizedPath '${as .isNotEmpty ? 'as $as ' : '' };" ;
144149
145150 if (! await customReporter.exists ()) return null ;
146151
@@ -159,3 +164,22 @@ Future<(String, String, bool)?> getCustomReporterImport(
159164
160165 return null ;
161166}
167+ /// This creates a relative path from `from` to `input` , the output being a
168+ /// posix path on all platforms.
169+ /// by talel briki 15/05/2025
170+ String _posixRelative (String input, {String from = "" }) {
171+ // Use the appropriate context for Windows
172+ final p.Context context = p.Context (style: p.Style .windows);
173+
174+ // Ensure the input and "from" paths are absolute
175+ final String absInputPath = File (input).absolute.path;
176+ final String absFromPath = Directory (from).absolute.path;
177+
178+ // Print the resolved absolute paths
179+
180+ // Compute the relative path
181+ final String relativePath = context.relative (absInputPath, from: absFromPath);
182+
183+ return relativePath;
184+ }
185+
0 commit comments