Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[web] [DDC] TypeError: Cannot read properties of undefined (reading 'new') #56498

Open
AlexV525 opened this issue Aug 16, 2024 · 17 comments
Open
Assignees
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler

Comments

@AlexV525
Copy link
Contributor

AlexV525 commented Aug 16, 2024

This tracker is for issues related to:

  • Dart native and web compilers
  • Dart VM

Upstream: cfug/dio#2282

We've split our Dio adapter with conditional imports. However, writing the caller in a specific way does not reference the correct constructor as I can tell.

Consider the example, running this on Flutter Web environment will unexpectedly throw TypeError: Cannot read properties of undefined (reading 'new'):

main.dart
import 'package:dio/dio.dart';
import 'package:flutter/material.dart';

import 'adapter.dart';

void main() {
  try {
    final adapter = makeHttpClientAdapter();
    print(adapter);
  } on Object catch (error) {
    print(error);
  }

  runApp(const MyApp());
}
adapter.dart
import 'package:dio/dio.dart';

import 'client_adapter_io.dart'
    if (dart.library.js_interop) 'client_adapter_web.dart' as adapter;

HttpClientAdapter makeHttpClientAdapter() => adapter.makeHttpClientAdapter();
adapter_io.dart
import 'package:dio/dio.dart';
import 'package:dio/io.dart';

HttpClientAdapter makeHttpClientAdapter() {
  return IOHttpClientAdapter();
}
adapter_web.dart
import 'package:dio/browser.dart';
import 'package:dio/dio.dart';

HttpClientAdapter makeHttpClientAdapter() {
  return BrowserHttpClientAdapter()..withCredentials = true;
}

Workaround

HttpClientAdapter makeHttpClientAdapter() {
  final adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
  adapter.withCredentials = true;
  return adapter;
}
@dart-github-bot
Copy link
Collaborator

Summary: The issue is a TypeError thrown when using conditional imports with Dio on Flutter Web. The error occurs because the correct constructor for the HttpClientAdapter is not being referenced due to the way the caller is written.

@dart-github-bot dart-github-bot added area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Aug 16, 2024
@AlexV525
Copy link
Contributor Author

@Rexios80 Do you have any clues what is going on here? 🤔

@Rexios80
Copy link
Contributor

What dart/flutter version? There might be some fixes that still aren't merged into stable.

@Dan-Crane
Copy link

@Rexios80 im faced this:

[✓] Flutter (Channel stable, 3.22.2, on macOS 14.5 23F79 darwin-arm64, locale en-RU)
[✓] Android toolchain - develop for Android devices (Android SDK version 33.0.1)
[✓] Xcode - develop for iOS and macOS (Xcode 15.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2023.2)
[✓] VS Code (version 1.92.2)
[✓] Connected device (5 available)
[✓] Network resources

@Rexios80
Copy link
Contributor

Can you reproduce on flutter master?

@Dan-Crane
Copy link

This also happens on master 3.24.0-1.0.pre.606

@Rexios80
Copy link
Contributor

Is it the cascade that's causing the issue?

@AlexV525
Copy link
Contributor Author

Is it the cascade that's causing the issue?

Nope. Removing it doesn't help.

@Rexios80
Copy link
Contributor

You say this is a workaround:

HttpClientAdapter makeHttpClientAdapter() {
  final adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
  adapter.withCredentials = true;
  return adapter;
}

What about this fixes the issue if it's not removing the cascade? Is it the cast?

@AlexV525
Copy link
Contributor Author

You say this is a workaround:

HttpClientAdapter makeHttpClientAdapter() {
  final adapter = HttpClientAdapter() as BrowserHttpClientAdapter;
  adapter.withCredentials = true;
  return adapter;
}

What about this fixes the issue if it's not removing the cascade? Is it the cast?

Yes, creating the adapter like this and force cast it does work, it's not about the cascade. The workaround is to help people to achieve their goal.

@nshahan
Copy link
Contributor

nshahan commented Aug 16, 2024

Which of the web environments are you experiencing this issue?

  • Production JavaScript (dart2js compiler)
  • Production WASM (dart2wasm compiler)
  • Development (DDC compiler)

@nshahan nshahan added the needs-info We need additional information from the issue author (auto-closed after 14 days if no response) label Aug 16, 2024
@lrhn lrhn removed the triage-automation See https://github.com/dart-lang/ecosystem/tree/main/pkgs/sdk_triage_bot. label Aug 16, 2024
@AlexV525
Copy link
Contributor Author

Which of the web environments are you experiencing this issue?

  • Development (DDC compiler)

The DDC compiler. The built Web application does not run into the issue.

@AlexV525 AlexV525 changed the title [web] TypeError: Cannot read properties of undefined (reading 'new') [web] [DDC] TypeError: Cannot read properties of undefined (reading 'new') Aug 17, 2024
@github-actions github-actions bot removed the needs-info We need additional information from the issue author (auto-closed after 14 days if no response) label Aug 17, 2024
@sigmundch
Copy link
Member

@nshahan - I recall we've seen an error with this message before long ago, but we didn't have a repro at hand. Maybe they are related? (see #50380)

@Rexios80
Copy link
Contributor

I actually have this error in one of my projects now. Not sure how it took me this long to notice. The workaround provided by @AlexV525 does fix the issue.

@AlexV525
Copy link
Contributor Author

This is getting worse according to cfug/dio#2282 (comment)

@nshahan @sigmundch Would you mind taking further investigations?

@ryanovas
Copy link

I'm getting this error using:

Flutter 3.29.0 • channel stable • https://github.com/flutter/flutter.git
Framework • revision 35c388afb5 (4 days ago) • 2025-02-10 12:48:41 -0800
Engine • revision f73bfc4522
Tools • Dart 3.7.0 • DevTools 2.42.2

I'm not even using conditional imports or anything. Simply trying to use BrowserHttpClientAdapter() at all throws this error.

@biggs0125 biggs0125 self-assigned this Feb 17, 2025
@biggs0125
Copy link

I have a fix in progress but to summarize the issue:
package:dio has a dependency on package:dio_web_adapter. Both packages include a file called package:<pkg_name>/browser.dart. package:dio/browser.dart is the library that contains BrowserHttpClientAdapter.

DDC compiles libraries modularly but it can sometimes combine libraries from different packages into the same JS module. In this case both package:dio_web_adapter/browser.dart and package:dio/browser.dart are getting compiled into the same JS module. DDC then tries to create an import/export nickname for each library in the module but gives both the same nickname. So due to this bug when adapter_web.dart imports package:dio/browser.dart, it ends up importing package:dio_web_adapter/browser.dart instead. This is why BrowserHttpClientAdapter seems to be missing.

The workaround to use HttpClientAdapter() instead calls a constructor from a different library, one that doesn't have a naming collision so we can successfully access it from adapter_web.dart. It then calls through to the BrowserHttpClientAdapter constructor from within the same module. Since the call is within the same JS module, the import nickname doesn't matter and so the code for HttpClientAdapter is successfully able to call BrowserHttpClientAdapter().

copybara-service bot pushed a commit that referenced this issue Feb 18, 2025
The attached bug shows an issue users have been encountering where a constructor seems to be undefined. It turns out this is because DDC is trying to read the constructor from the wrong library.

This is happening because both 'package:dio' and a sister package 'package:dio_web_adapter' both contain a library with the same path: 'src/adapter.dart'. 'BrowserHttpClientAdapter' the class they are trying to reference is defined in 'package:dio/src/adapter.dart'. However, due to a naming collision, their import is referencing 'package:dio_web_adapter/src/adapter.dart'.

This naming collision happens because of the logic in '_jsLibraryAlias'. By truncating the start of the import URI (i.e. 'dio/' and 'dio_web_adapter/') the two libraries map to the same alias. This alias is then used to as the key in the AMD module export object and since both libraries are in the same module, only the second one gets exported.

This code may have been written with the assumption that libraries from different packages would always be in different modules (in which case the shortened paths shouldn't collide) but this is not the case. The fix is to use the full import URI including the package name.

In writing the attached modular test I discovered another issue that only affects es6 imports. The ScopedId resolver was not considering NameSpecifier as a declaration point for variables. This lead to a similar name collision since the import alias's name was also being derived from a truncated import URI. In the test, both 'f1/foo.dart' and 'f2/foo.dart' were being imported 'as foo'. Now one is 'as foo' and the other is 'as foo$'.

The first issue affects both AMD and es6 while the second issue only affects es6. The modular tests run with es6 so the new test fails if either of these fixes is not in place.

The new DDC module system is not affected by either issue since it doesn't use NameSpecifiers and it uses the full import URI as a string to register libraries rather than a shortened alias.

Tested on TGP and with a local Flutter application.

Bug: #56498
Change-Id: I5bdb945cfbe615874b40e2fc4ebba31b661cf3b7
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/410260
Reviewed-by: Nicholas Shahan <nshahan@google.com>
Commit-Queue: Nate Biggs <natebiggs@google.com>
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
area-web Use area-web for Dart web related issues, including the DDC and dart2js compilers and JS interop. type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) web-dev-compiler
Projects
None yet
Development

No branches or pull requests

9 participants