From 062fadf072bc67c4990fc41e7bdc11623642af69 Mon Sep 17 00:00:00 2001 From: Gurleen-kansary Date: Fri, 30 Jan 2026 01:42:37 +0530 Subject: [PATCH 1/3] Fix potential null access for _tmpDir in temp dir creation --- pkgs/ffigen/lib/src/strings.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkgs/ffigen/lib/src/strings.dart b/pkgs/ffigen/lib/src/strings.dart index 4e5c18cf11..bca46329a5 100644 --- a/pkgs/ffigen/lib/src/strings.dart +++ b/pkgs/ffigen/lib/src/strings.dart @@ -289,8 +289,8 @@ String get tmpDir { return Platform.environment['TEST_TMPDIR']!; } - _tmpDir ??= Directory.systemTemp.createTempSync(); - return _tmpDir!.path; + _tmpDir ??= Directory.systemTemp.createTempSync('temp dir '); + return _tmpDir!.path; } const ffigenJsonSchemaIndent = ' '; From d3b1463cc65fcb2ba44f9be8009132b757781806 Mon Sep 17 00:00:00 2001 From: Gurleen-kansary Date: Thu, 5 Feb 2026 23:47:43 +0530 Subject: [PATCH 2/3] fix: handle tmpdir null issue in ffigen context --- pkgs/ffigen/lib/src/context.dart | 42 ++++++++++++------- .../code_generator_test.dart | 3 +- .../decl_decl_collision_test.dart | 2 + .../decl_symbol_address_collision_test.dart | 2 + .../decl_type_name_collision_test.dart | 2 + .../reserved_keyword_collision_test.dart | 2 + .../example_tests/cjson_example_test.dart | 3 +- .../example_tests/ffinative_example_test.dart | 5 ++- .../example_tests/libclang_example_test.dart | 2 +- .../shared_bindings_example_test.dart | 10 +++-- .../example_tests/simple_example_test.dart | 5 ++- .../comment_markup_test.dart | 2 + .../header_parser_tests/dart_handle_test.dart | 2 + .../enum_int_mimic_test.dart | 2 + .../forward_decl_test.dart | 2 + .../header_parser_tests/functions_test.dart | 2 + .../imported_types_test.dart | 2 + .../native_func_typedef_test.dart | 2 + .../opaque_dependencies_test.dart | 2 + .../packed_structs_test.dart | 2 + .../header_parser_tests/regress_384_test.dart | 2 + .../test/header_parser_tests/sort_test.dart | 3 +- .../struct_fptr_fields_test.dart | 2 + .../header_parser_tests/typedef_test.dart | 2 + .../test/header_parser_tests/unions_test.dart | 2 + .../header_parser_tests/varargs_test.dart | 2 + .../large_integration_tests/large_test.dart | 12 ++++-- pkgs/ffigen/test/test_utils.dart | 12 ++++-- 28 files changed, 97 insertions(+), 36 deletions(-) diff --git a/pkgs/ffigen/lib/src/context.dart b/pkgs/ffigen/lib/src/context.dart index 4b2eae002a..ddc55f70f8 100644 --- a/pkgs/ffigen/lib/src/context.dart +++ b/pkgs/ffigen/lib/src/context.dart @@ -31,22 +31,31 @@ class Context { final Scope rootScope = Scope.createRoot('root'); final Scope rootObjCScope = Scope.createRoot('objc_root'); late final ExtraSymbols extraSymbols; - - Context(this.logger, FfiGenerator generator, {Uri? libclangDylib}) - : config = Config(generator), - cursorIndex = CursorIndex(logger) { - objCBuiltInFunctions = ObjCBuiltInFunctions( - this, - // ignore: deprecated_member_use_from_same_package - generator.objectiveC?.generateForPackageObjectiveC ?? false, - ); - final libclangDylibPath = - // ignore: deprecated_member_use_from_same_package - generator.libclangDylib?.toFilePath() ?? - libclangDylib?.toFilePath() ?? - findDylibAtDefaultLocations(logger); - _clang ??= Clang(DynamicLibrary.open(libclangDylibPath)); - } + final String tmpDir; + + Context( + this.logger, + FfiGenerator generator, { + Uri? libclangDylib, + String? tmpDir, // optional parameter +}) : config = Config(generator), + cursorIndex = CursorIndex(logger), + tmpDir = tmpDir ?? Directory.systemTemp.createTempSync('ffigen temp dir ').path // default temp dir with spaces +{ + // Initialize compiler options + compilerOpts = config.headers.compilerOptions ?? defaultCompilerOpts(logger); + + objCBuiltInFunctions = ObjCBuiltInFunctions( + this, + generator.objectiveC?.generateForPackageObjectiveC ?? false, + ); + + final libclangDylibPath = + generator.libclangDylib?.toFilePath() ?? + libclangDylib?.toFilePath() ?? + findDylibAtDefaultLocations(logger); + + _clang ??= Clang(DynamicLibrary.open(libclangDylibPath)); } /// The clang bindings. @@ -123,3 +132,4 @@ typedef ExtraSymbols = ({ // TODO(https://github.com/dart-lang/native/issues/1259): Make this nullable. Symbol symbolAddressVariableName, }); +} \ No newline at end of file diff --git a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart index 2a993d1e2d..02c82b94ff 100644 --- a/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart +++ b/pkgs/ffigen/test/code_generator_tests/code_generator_test.dart @@ -789,7 +789,8 @@ void main() { /// Utility to match expected bindings to the generated bindings. void _matchLib(Library lib, String testName) { - matchLibraryWithExpected(lib, 'code_generator_test_${testName}_output.dart', [ + final context = testContext(); + matchLibraryWithExpected(context, lib, 'code_generator_test_${testName}_output.dart', [ 'test', 'code_generator_tests', 'expected_bindings', diff --git a/pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart b/pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart index 12404a2957..ebb82106be 100644 --- a/pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart +++ b/pkgs/ffigen/test/collision_tests/decl_decl_collision_test.dart @@ -82,7 +82,9 @@ void main() { ), ], context), ); + final context = testContext(); matchLibraryWithExpected( + context, library, 'decl_decl_collision_test_output.dart', [ diff --git a/pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart b/pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart index 40fd097970..fce49fd9c5 100644 --- a/pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart +++ b/pkgs/ffigen/test/collision_tests/decl_symbol_address_collision_test.dart @@ -49,7 +49,9 @@ void main() { ); }); test('declaration and symbol address conflict', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'collision_test_decl_symbol_address_collision_output.dart', [ diff --git a/pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart b/pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart index 4c811cbda1..5a9626ee5b 100644 --- a/pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart +++ b/pkgs/ffigen/test/collision_tests/decl_type_name_collision_test.dart @@ -28,7 +28,9 @@ ${strings.headers}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'decl_type_name_collision_test_output.dart', [ diff --git a/pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart b/pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart index 6512f992bc..cc5e3a8195 100644 --- a/pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart +++ b/pkgs/ffigen/test/collision_tests/reserved_keyword_collision_test.dart @@ -12,6 +12,7 @@ import '../test_utils.dart'; void main() { group('reserved_keyword_collision_test', () { test('reserved keyword collision', () { + final context = testContext(); final library = parser.parse( testContext( FfiGenerator( @@ -45,6 +46,7 @@ void main() { ), ); matchLibraryWithExpected( + context, library, 'reserved_keyword_collision_test_output.dart', [ diff --git a/pkgs/ffigen/test/example_tests/cjson_example_test.dart b/pkgs/ffigen/test/example_tests/cjson_example_test.dart index 2fc1c43bce..3f06da40b4 100644 --- a/pkgs/ffigen/test/example_tests/cjson_example_test.dart +++ b/pkgs/ffigen/test/example_tests/cjson_example_test.dart @@ -14,9 +14,10 @@ void main() { final config = testConfigFromPath( path.join(packagePathForTests, 'example', 'c_json', 'config.yaml'), ); + final context = testContext(config); final library = parse(testContext(config)); - matchLibraryWithExpected(library, 'example_c_json.dart', [ + matchLibraryWithExpected(context, library, 'example_c_json.dart', [ config.output.dartFile.toFilePath(), ]); }); diff --git a/pkgs/ffigen/test/example_tests/ffinative_example_test.dart b/pkgs/ffigen/test/example_tests/ffinative_example_test.dart index 5e70ca1143..61c2431f35 100644 --- a/pkgs/ffigen/test/example_tests/ffinative_example_test.dart +++ b/pkgs/ffigen/test/example_tests/ffinative_example_test.dart @@ -14,9 +14,10 @@ void main() { final config = testConfigFromPath( path.join(packagePathForTests, 'example', 'ffinative', 'config.yaml'), ); - final library = parse(testContext(config)); + final context = testContext(config); + final library = parse(context); - matchLibraryWithExpected(library, 'example_ffinative.dart', [ + matchLibraryWithExpected(context, library, 'example_ffinative.dart', [ config.output.dartFile.toFilePath(), ]); }); diff --git a/pkgs/ffigen/test/example_tests/libclang_example_test.dart b/pkgs/ffigen/test/example_tests/libclang_example_test.dart index 4435bcd36c..22dd0a0c0e 100644 --- a/pkgs/ffigen/test/example_tests/libclang_example_test.dart +++ b/pkgs/ffigen/test/example_tests/libclang_example_test.dart @@ -35,7 +35,7 @@ void main() { final context = testContext(generator); final library = parse(context); - matchLibraryWithExpected(library, 'example_libclang.dart', [ + matchLibraryWithExpected(context, library, 'example_libclang.dart', [ generator.output.dartFile.toFilePath(), ]); }); diff --git a/pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart b/pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart index f950b9f669..f7572422fb 100644 --- a/pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart +++ b/pkgs/ffigen/test/example_tests/shared_bindings_example_test.dart @@ -20,9 +20,9 @@ void main() { 'a_shared_base.yaml', ), ); - final library = parse(testContext(config)); - - matchLibraryWithExpected(library, 'example_shared_bindings.dart', [ + final context = testContext(config); + final library = parse(context); + matchLibraryWithExpected(context, library, 'example_shared_bindings.dart', [ config.output.dartFile.toFilePath(), ]); }); @@ -37,8 +37,10 @@ void main() { 'base.yaml', ), ); - final library = parse(testContext(config)); + final context = testContext(config); + final library = parse(context); matchLibrarySymbolFileWithExpected( + context, library, 'example_shared_bindings.yaml', [config.output.symbolFile!.output.toFilePath()], diff --git a/pkgs/ffigen/test/example_tests/simple_example_test.dart b/pkgs/ffigen/test/example_tests/simple_example_test.dart index 4d5fee6394..f449ff85a5 100644 --- a/pkgs/ffigen/test/example_tests/simple_example_test.dart +++ b/pkgs/ffigen/test/example_tests/simple_example_test.dart @@ -14,9 +14,10 @@ void main() { final config = testConfigFromPath( path.join(packagePathForTests, 'example', 'simple', 'config.yaml'), ); - final library = parse(testContext(config)); + final context = testContext(config); + final library = parse(context); - matchLibraryWithExpected(library, 'example_simple.dart', [ + matchLibraryWithExpected(context, library, 'example_simple.dart', [ config.output.dartFile.toFilePath(), ]); }); diff --git a/pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart b/pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart index 80cbd8f37b..7d3488c01d 100644 --- a/pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/comment_markup_test.dart @@ -31,7 +31,9 @@ ${strings.comments}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_comment_markup_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart b/pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart index 8148c9a41c..226582b709 100644 --- a/pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/dart_handle_test.dart @@ -34,7 +34,9 @@ ${strings.headers}: ); }); test('Expected Bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_dart_handle_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart b/pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart index 783427b602..0c52231f48 100644 --- a/pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/enum_int_mimic_test.dart @@ -31,7 +31,9 @@ ${strings.ignoreSourceErrors}: true }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_enum_int_mimic_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart b/pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart index ac4d12d773..d70eb0ac2a 100644 --- a/pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/forward_decl_test.dart @@ -29,7 +29,9 @@ ${strings.ignoreSourceErrors}: true }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_forward_decl_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/functions_test.dart b/pkgs/ffigen/test/header_parser_tests/functions_test.dart index 664516593c..99d6721778 100644 --- a/pkgs/ffigen/test/header_parser_tests/functions_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/functions_test.dart @@ -40,7 +40,9 @@ ${strings.functions}: ); }); test('Expected Bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_functions_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/imported_types_test.dart b/pkgs/ffigen/test/header_parser_tests/imported_types_test.dart index 4de99982c5..6b1d4fb800 100644 --- a/pkgs/ffigen/test/header_parser_tests/imported_types_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/imported_types_test.dart @@ -31,7 +31,9 @@ ${strings.headers}: ); }); test('Expected Bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_imported_types_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/native_func_typedef_test.dart b/pkgs/ffigen/test/header_parser_tests/native_func_typedef_test.dart index 049af1f3cc..39583399cd 100644 --- a/pkgs/ffigen/test/header_parser_tests/native_func_typedef_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/native_func_typedef_test.dart @@ -35,7 +35,9 @@ ${strings.headers}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_native_func_typedef_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/opaque_dependencies_test.dart b/pkgs/ffigen/test/header_parser_tests/opaque_dependencies_test.dart index 14955ee0ca..9449aebeba 100644 --- a/pkgs/ffigen/test/header_parser_tests/opaque_dependencies_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/opaque_dependencies_test.dart @@ -35,7 +35,9 @@ ${strings.unions}: ); }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_opaque_dependencies_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/packed_structs_test.dart b/pkgs/ffigen/test/header_parser_tests/packed_structs_test.dart index 188cf5b8e9..bc00429bc7 100644 --- a/pkgs/ffigen/test/header_parser_tests/packed_structs_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/packed_structs_test.dart @@ -28,7 +28,9 @@ ${strings.headers}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_packed_structs_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/regress_384_test.dart b/pkgs/ffigen/test/header_parser_tests/regress_384_test.dart index a4aeb7a76e..59177ece5d 100644 --- a/pkgs/ffigen/test/header_parser_tests/regress_384_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/regress_384_test.dart @@ -29,7 +29,9 @@ ${strings.headers}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_regress_384_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/sort_test.dart b/pkgs/ffigen/test/header_parser_tests/sort_test.dart index a1f8bcb7e3..2833a056e6 100644 --- a/pkgs/ffigen/test/header_parser_tests/sort_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/sort_test.dart @@ -42,7 +42,8 @@ void main() { ); }); test('Expected Bindings', () { - matchLibraryWithExpected(actual, 'header_parser_sort_test_output.dart', [ + final context = testContext(); + matchLibraryWithExpected(context, actual, 'header_parser_sort_test_output.dart', [ 'test', 'header_parser_tests', 'expected_bindings', diff --git a/pkgs/ffigen/test/header_parser_tests/struct_fptr_fields_test.dart b/pkgs/ffigen/test/header_parser_tests/struct_fptr_fields_test.dart index c19409e9af..c319f1d3b6 100644 --- a/pkgs/ffigen/test/header_parser_tests/struct_fptr_fields_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/struct_fptr_fields_test.dart @@ -34,7 +34,9 @@ ${strings.headers}: }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_struct_fptr_fields_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/typedef_test.dart b/pkgs/ffigen/test/header_parser_tests/typedef_test.dart index 45cbb00c97..ddbb8800a1 100644 --- a/pkgs/ffigen/test/header_parser_tests/typedef_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/typedef_test.dart @@ -48,7 +48,9 @@ ${strings.preamble}: | }); test('Expected Bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_typedef_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/unions_test.dart b/pkgs/ffigen/test/header_parser_tests/unions_test.dart index b9708a2619..af8165e295 100644 --- a/pkgs/ffigen/test/header_parser_tests/unions_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/unions_test.dart @@ -29,7 +29,9 @@ ${strings.ignoreSourceErrors}: true }); test('Expected bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_unions_test_output.dart', [ diff --git a/pkgs/ffigen/test/header_parser_tests/varargs_test.dart b/pkgs/ffigen/test/header_parser_tests/varargs_test.dart index 816ec8894d..b07364235f 100644 --- a/pkgs/ffigen/test/header_parser_tests/varargs_test.dart +++ b/pkgs/ffigen/test/header_parser_tests/varargs_test.dart @@ -49,7 +49,9 @@ ${strings.functions}: ); }); test('Expected Bindings', () { + final context = testContext(); matchLibraryWithExpected( + context, actual, 'header_parser_varargs_test_output.dart', [ diff --git a/pkgs/ffigen/test/large_integration_tests/large_test.dart b/pkgs/ffigen/test/large_integration_tests/large_test.dart index 68fdd2f36b..e2d52b6289 100644 --- a/pkgs/ffigen/test/large_integration_tests/large_test.dart +++ b/pkgs/ffigen/test/large_integration_tests/large_test.dart @@ -74,8 +74,10 @@ void main() { ), ); final library = parse(Context(logger, generator)); + final context = testContext(); matchLibraryWithExpected( + context, library, 'large_test_libclang.dart', ['test', 'large_integration_tests', '_expected_libclang_bindings.dart'], @@ -151,9 +153,10 @@ void main() { macros: Macros.includeAll, typedefs: Typedefs.includeAll, ); - final library = parse(testContext(generator)); + final context = testContext(generator); + final library = parse(context); - matchLibraryWithExpected(library, 'large_test_cjson.dart', [ + matchLibraryWithExpected(context, library, 'large_test_cjson.dart', [ 'test', 'large_integration_tests', '_expected_cjson_bindings.dart', @@ -202,9 +205,10 @@ void main() { include: (declaration) => !vaRegex.hasMatch(declaration.originalName), ), ); - final library = parse(testContext(generator)); + final context = testContext(generator); + final library = parse(context); - matchLibraryWithExpected(library, 'large_test_sqlite.dart', [ + matchLibraryWithExpected(context, library, 'large_test_sqlite.dart', [ 'test', 'large_integration_tests', '_expected_sqlite_bindings.dart', diff --git a/pkgs/ffigen/test/test_utils.dart b/pkgs/ffigen/test/test_utils.dart index d5665b1c89..33b0970c8f 100644 --- a/pkgs/ffigen/test/test_utils.dart +++ b/pkgs/ffigen/test/test_utils.dart @@ -110,6 +110,7 @@ String _normalizeGeneratedCode( /// /// This will not delete the actual debug file incase [expect] throws an error. void matchLibraryWithExpected( + Context context, Library library, String pathForActual, List pathToExpected, { @@ -117,6 +118,7 @@ void matchLibraryWithExpected( bool format = true, }) { _matchFileWithExpected( + context: context, library: library, pathForActual: pathForActual, pathToExpected: pathToExpected, @@ -130,12 +132,14 @@ void matchLibraryWithExpected( /// /// This will not delete the actual debug file incase [expect] throws an error. void matchLibrarySymbolFileWithExpected( + Context context, Library library, String pathForActual, List pathToExpected, String importPath, ) { _matchFileWithExpected( + context: context, library: library, pathForActual: pathForActual, pathToExpected: pathToExpected, @@ -155,13 +159,11 @@ String absPath(String p) => path.join(packagePathForTests, p); String configPath(String directory, String file) => absPath(configPathForTest(directory, file)); -/// Returns the temp directory used to store bindings generated by tests. -String tmpDir = path.join(packagePathForTests, 'test', '.temp'); - /// Generates actual file using library and tests using [expect] with expected. /// /// This will not delete the actual debug file incase [expect] throws an error. void _matchFileWithExpected({ + required Context context, required Library library, required String pathForActual, required List pathToExpected, @@ -170,7 +172,9 @@ void _matchFileWithExpected({ String Function(String)? codeNormalizer, }) { final expectedPath = path.joinAll([packagePathForTests, ...pathToExpected]); - final file = File(path.join(tmpDir, pathForActual)); + final tmpDir = Directory.systemTemp.createTempSync('ffigen_test_'); + final file = File(path.join(tmpDir.path, pathForActual)); + fileWriter(library: library, file: file); try { final actual = _normalizeGeneratedCode( From 836e6a1c82920b05884ed18d647d39a507688776 Mon Sep 17 00:00:00 2001 From: Gurleen-kansary Date: Sat, 7 Feb 2026 17:10:43 +0530 Subject: [PATCH 3/3] Move temp directory handling into Context; remove global _tmpDir --- pkgs/ffigen/lib/src/strings.dart | 13 ------------- pkgs/ffigen/test/test_utils.dart | 6 +++--- 2 files changed, 3 insertions(+), 16 deletions(-) diff --git a/pkgs/ffigen/lib/src/strings.dart b/pkgs/ffigen/lib/src/strings.dart index bca46329a5..8258f7a1a8 100644 --- a/pkgs/ffigen/lib/src/strings.dart +++ b/pkgs/ffigen/lib/src/strings.dart @@ -280,19 +280,6 @@ const synthUsrChar = '~'; const ffiNative = 'ffi-native'; const ffiNativeAsset = 'asset-id'; -Directory? _tmpDir; - -/// A path to a unique temporary directory that should be used for files meant -/// to be discarded after the current execution is finished. -String get tmpDir { - if (Platform.environment.containsKey('TEST_TMPDIR')) { - return Platform.environment['TEST_TMPDIR']!; - } - - _tmpDir ??= Directory.systemTemp.createTempSync('temp dir '); - return _tmpDir!.path; -} - const ffigenJsonSchemaIndent = ' '; const ffigenJsonSchemaId = 'https://json.schemastore.org/ffigen'; const ffigenJsonSchemaFileName = 'ffigen.schema.json'; diff --git a/pkgs/ffigen/test/test_utils.dart b/pkgs/ffigen/test/test_utils.dart index 33b0970c8f..07e4db9c45 100644 --- a/pkgs/ffigen/test/test_utils.dart +++ b/pkgs/ffigen/test/test_utils.dart @@ -172,8 +172,8 @@ void _matchFileWithExpected({ String Function(String)? codeNormalizer, }) { final expectedPath = path.joinAll([packagePathForTests, ...pathToExpected]); - final tmpDir = Directory.systemTemp.createTempSync('ffigen_test_'); - final file = File(path.join(tmpDir.path, pathForActual)); + final tmpDirPath = context.tmpDir; + final file = File(path.join(tmpDirPath, pathForActual)); fileWriter(library: library, file: file); try { @@ -245,4 +245,4 @@ FfiGenerator testConfigFromPath(String path, {Logger? logger}) { return testConfig(yamlBody, filename: path, logger: logger); } -bool isFlutterTester = Platform.resolvedExecutable.contains('flutter_tester'); +bool isFlutterTester = Platform.resolvedExecutable.contains('flutter_tester'); \ No newline at end of file